我在自定义列表上创建了标准 SharePoint 2013 事件接收器。
观看的事件=“项目已添加”。
稍后在我的代码中,我需要按照用户插入列表项的顺序检索列表项的附件。但不幸的是,SharePoint 默认情况下似乎不这样做。
示例:
用户创建一个列表项并附加以下文件
图片_正面.jpg
图片_back.png
图片_231.jpg
现在在我的事件接收器中,我可能首先收到“Picture_back”,然后收到“Picture_front”...或以任何其他顺序。
如何按照附加到列表项的顺序检索附件? 我尝试使用 SPFile 属性“TimeCreated”,但这也不起作用...它们具有相同的时间戳:((如果我使用“Ticks”)
有什么想法或者我做错了什么吗?
这是我的代码:
public override void ItemAdded(SPItemEventProperties properties)
{
SPAttachmentCollection attachments = properties.ListItem.Attachments;
if (attachments.Count > 0)
{
int p = 1;
Dictionary<string, string> attachementDict = new Dictionary<string, string>();
try
{
foreach (string attachement in attachments)
{
SPFile attachementFile = properties.ListItem.ParentList.ParentWeb.GetFile(properties.ListItem.Attachments.UrlPrefix + attachement);
string imageUrlPath = properties.WebUrl + attachementFile.ServerRelativeUrl;
string imageTimestamp = attachementFile.TimeCreated.Ticks.ToString();
// This Dict is used lator for sorting
// but at the Moment I get here the error that the same key already exists because of the same timestamp of the files :(
attachementDict.Add(imageTimestamp, imageUrlPath);
}
}
catch (Exception ex)
{
// SPLog
}
}
这是我的代码..希望它对您有帮助!
try
{
string strUrl = SPContext.Current.Site.Url + "/" + subSite;
using (SPSite Site = new SPSite(strUrl))
{
using (SPWeb Web = Site.OpenWeb())
{
SPList List = Web.Lists[listName];
SPListItem item = List.GetItemById(ID);
foreach (String attachmentname in item.Attachments)
{
AnnouncementsCommon objAnnouncementsCommon = new AnnouncementsCommon();
String attachmentAbsoluteURL = item.Attachments.UrlPrefix + attachmentname;
objAnnouncementsCommon.AttachmentName = attachmentname;
objAnnouncementsCommon.AttachmentURL = attachmentAbsoluteURL;
lstAnnouncementsCommon.Add(objAnnouncementsCommon);
}
}
}
}
catch (Exception Exc)
{
Microsoft.Office.Server.Diagnostics.PortalLog.LogString("SSC DAL Exception Occurred: {0} || {1}", Exc.Message, Exc.StackTrace);
}
return lstAnnouncementsCommon;
}
作为另一种方法,您可以使用接收器将附件存储在图片库中,并向该库添加两个字段:原始自定义列表项的查找列和包含“默认”、“前视图”、“后视图”(或类似的)。
一个优点是您将来可以轻松更新图像,另一个优点是 SharePoint 会自动为您的图像创建两个尺寸合适的预览微型图,从而减少带宽。