This topic has not yet been rated - Rate this topic

SummaryLink Class

A SummaryLink object that provides a way to store Web addresses.

System.Object
  Microsoft.SharePoint.Publishing.SummaryLink

Namespace:  Microsoft.SharePoint.Publishing
Assembly:  Microsoft.SharePoint.Publishing (in Microsoft.SharePoint.Publishing.dll)
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public sealed class SummaryLink

A SummaryLink is an annotated hyperlink.

At minimum, a SummaryLink object contains a titleand a linkUrl. It also contains styling information that affects how it is rendered by the SummaryLinkFieldControl or SummaryLinkWebPart.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Publishing.Fields;
using Microsoft.SharePoint.Publishing.WebControls;
using SharePointWebParts = Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls.WebParts;

namespace SummaryLinkFieldTest
{
    static class CreateSummaryLinks
    {
        /// Creates SummaryLinks in a SummaryLinkField within a PublishingPage
        /// This sample creates group headers and summary links within a SummaryLinkFieldValue object
        /// and then applies the SummaryLinkFieldValue to a SummaryLinkField within a Page.
        /// 
        /// Pre-requisite:
        /// This sample assumes that 'Content Approval' is applied to 
        /// the Pages document library containing the PublishingPage.
        /// <param name="publishingPage">The page in which to create the summary links</param>
        /// <param name="fieldName">The name of the SummaryLinkField</param>
        public static void createFieldControlSummaryLinks(PublishingPage publishingPage, string fieldName)
        {
            // SummaryLinkFieldValue is the container for SummaryLinks and display configuration
            // data.
            SummaryLinkFieldValue summaryLinkValue = CreateSummaryLinks.createSummaryLinkValue();

            if (publishingPage.ListItem.File.CheckOutStatus == SPFile.SPCheckOutStatus.None)
            {
                publishingPage.CheckOut();
            }
            
            // Replace the existing contents of the field with the newly created SummaryLinkValue
            publishingPage.ListItem[fieldName] = summaryLinkValue;

            publishingPage.Update();
            publishingPage.CheckIn("Checking in summary link change");
            publishingPage.ListItem.File.Publish("Publishing summary link change");
            publishingPage.ListItem.File.Approve("Approving summary link change");
        }

        
        /// Creates SummaryLinks in a SummaryLinkWebPart within a PublishingPage
        /// This sample creates group headers and summary links within a SummaryLinkFieldValue object
        /// and then applies the SummaryLinkFieldValue to a SummaryLinkWebPart on a PublishingPage.
        /// 
        /// Pre-requisite:
        /// This sample assumes that 'Content Approval' is applied to 
        /// the Pages document library containing the PublishingPage.
        /// <param name="publishingPage">The page in which to create the summary links</param>
        /// <param name="webRelativePageUrl">The web relative URL of the page</param>
        /// <param name="webPartTitle">The Title of the web part</param>
        public static void createWebPartSummaryLinks(PublishingPage publishingPage, string webRelativePageUrl, string webPartTitle)
        {
            SummaryLinkFieldValue summaryLinkValue = CreateSummaryLinks.createSummaryLinkValue();

            if (publishingPage.ListItem.File.CheckOutStatus == SPFile.SPCheckOutStatus.None)
            {
                publishingPage.CheckOut();
            }

            SharePointWebParts.SPLimitedWebPartManager webPartManager =
                publishingPage.ListItem.Web.GetLimitedWebPartManager(webRelativePageUrl,
                                                          System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

            SharePointWebParts.SPLimitedWebPartCollection webParts = webPartManager.WebParts;

            foreach (WebPart webPart in webParts)
            {
                SummaryLinkWebPart slwp = webPart as SummaryLinkWebPart;
                if (slwp != null && 
                    string.CompareOrdinal(slwp.Title, webPartTitle) == 0)
                {
                    // Replace the existing contents of the Web part with the newly created SummaryLinkValue
                    slwp.SummaryLinkValue = summaryLinkValue;
                    webPartManager.SaveChanges(slwp);
                    break;
                }
            }

            publishingPage.Update();
            publishingPage.CheckIn("Checking in summary link change");
            publishingPage.ListItem.File.Publish("Publishing summary link change");
            publishingPage.ListItem.File.Approve("Approving summary link change");
        }

        public static SummaryLinkFieldValue createSummaryLinkValue()
        {
            SummaryLinkFieldValue summaryLinkValue = new SummaryLinkFieldValue();
            summaryLinkValue.DisplayColumns = 2;
            summaryLinkValue.LinkStyle = "Bullets";
            summaryLinkValue.GroupStyle = "Black";

            SummaryLinkCollection summaryLinks = summaryLinkValue.SummaryLinks;

            SummaryLink summaryLink;

            // Add Groups and links.
            // The link style set here must correspond to one of the template names in the XSL Item style
            // This is not verified by the OM, but an incorrect template name results in the
            // link not rendering.
            
            // GroupA

            summaryLink = summaryLinks.Add("GroupA");
            summaryLink.IsGroupHeader = true;

            summaryLink = summaryLinks.Add("Link1");
            summaryLink.Description = "Description1";
            summaryLink.Style = "Default";
            summaryLink.ImageUrl = "http://www.live.com/live/1.100.7.337/img/wl_jewel.gif";
            summaryLink.LinkUrl = "http://www.example.com/link1";

            summaryLink = summaryLinks.Add("Link2");
            summaryLink.Description = "Description2";
            summaryLink.Style = "Default";
            summaryLink.ImageUrl = "http://www.live.com/live/1.100.7.337/img/wl_jewel.gif";
            summaryLink.LinkUrl = "http://www.example.com/link2";

            // GroupB

            summaryLink = summaryLinks.Add("GroupB");
            summaryLink.IsGroupHeader = true;

            summaryLink = summaryLinks.Add("Link3");
            summaryLink.Description = "Description3";
            summaryLink.Style = "Default";
            summaryLink.ImageUrl = "http://www.live.com/live/1.100.7.337/img/wl_jewel.gif";
            summaryLink.LinkUrl = "http://www.example.com/link3";
            summaryLink.LinkToolTip = "Tooltip for link3";

            return summaryLinkValue;
        }
    
    }
}
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Need some 2010 Updates to remove obsolete code
Checkout test should be: 
publishingPage.ListItem.File.CheckOutType == SPFile.SPCheckOutType.None