SharePoint 2010自定义列表和附件

时间:2014-12-15 11:07:38

标签: sharepoint-2010 attachment custom-lists sharepoint-api

我创建了包含少量列的自定义列表:

Title
Description
HrefLink

并可选择将附件上传到列表项。

我的方案如下,从列表中获取数据并使用超链接打印列表中的数据。在这里,对于超链接,我应该附加任何附件,其他项目也可以拉取HrefLink字段值。

如何找到列表项是否有附件,如何拉出附件路径并将其打印出来?

1 个答案:

答案 0 :(得分:0)

我已使用以下登录信息获取附件并打印到文字:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
     using (SPSite currentSite = new SPSite(siteUrl))
         {
             using (SPWeb currentWeb = currentSite.OpenWeb())
                    {

                        /*Footer - Hotspot Links */
                        //Define string builder to attach the fetched content
                        StringBuilder strFooterHotspotLinks = new StringBuilder();

                        SPList lstFooterHotspotLinks = currentWeb.Lists["Footer Hotspot Links"];

                        //Difine list query
                        SPQuery spQryFooterHotspotLinks = new SPQuery();
                        spQryFooterHotspotLinks.Query = "<OrderBy><FieldRef Name='Hotspot_x0020_Order' Ascending='True' /></OrderBy>";
                        spQryFooterHotspotLinks.RowLimit = 5;

                        SPListItemCollection lstItmFooterHotspotLinks = lstFooterHotspotLinks.GetItems(spQryFooterHotspotLinks);

                        int lnkCount = 1;

                        if (lstItmFooterHotspotLinks.Count > 0)
                        {
                            foreach (SPListItem itmFooterHotspotLinks in lstItmFooterHotspotLinks)
                            {

                                String qlAttachmentAbsUrl = itmFooterHotspotLinks.Attachments.UrlPrefix;    //gets the containing directory URl
                                SPAttachmentCollection qlAttachments = itmFooterHotspotLinks.Attachments;   //check the attachment exists or not for list item

                                //If list attachmetns are existing
                                if (qlAttachments.Count > 0)
                                {
                                    //Loop the list to find the attachments exist ot not and attach to the link
                                    foreach (String qlAttachmentName in qlAttachments)
                                    {
                                        strFooterHotspotLinks.Append("<li><a href='" + qlAttachmentAbsUrl + qlAttachmentName + "'><span class='icn-" + lnkCount + "'></span>" + itmFooterHotspotLinks["Hotspot Title"] + "</a></li>");
                                        lnkCount++;
                                    }
                                }
                                else
                                {
                                    strFooterHotspotLinks.Append("<li><a href='" + itmFooterHotspotLinks["Hotspot Link"] + "'><span class='icn-" + lnkCount + "'></span>" + itmFooterHotspotLinks["Hotspot Title"] + "</a></li>");
                                    lnkCount++;
                                }
                            }
                        }