Responsive Ads Here

Friday, December 26, 2014

Adding Content Editor Web Part to Page in SharePoint programmatically

Content Editor Web Part is one of  useful OOB web part in SharePoint. 

You can use the Content Editor Web Part to add formatted text, tables, hyperlinks, and images to a Web Part Page.
  • The Content Editor Web Part is intended for adding HTML content to a Web Part Page, which may include hyperlinks. However, this Web Part is not designed to connect to a Web site. If you need to connect a Web Part to a Web site, consider using the Page Viewer Web Part.
  • The Content Editor Web Part does not accept the HTML FORM element. If you need to add a Web Part that uses the FORM element, consider using the Page Viewer Web Part or the Form Web Part.
 In my recent project i have a requirement to add content editor web part to the page programmatically.  We can add content editor web part  manually to page, but when we are moving from one environment to another environment its difficult to add to page manually.

In feature activation event i have written the below code snippet which will add the content editor web part to the page.   

/// Page Url for the Page
/// Web Part name for the same
/// Web Part height for the same
/// Zone id for the same
/// Zone index for the same
/// SPWeb Object
public static void AddContentEditorWebPartToPage(string pageUrl, string webpartName, string webpartHeight, string zone, int zoneId, SPWeb web)
{
            // TO DO : Check for existence of web part
   SPFile page = default(SPFile);
   bool webpartExistance = false;
   try
   {
     page = web.GetFile(pageUrl);
     if (page.Exists)
     {
       if (page.CheckOutType == SPFile.SPCheckOutType.Online)
       {
          throw new Exception("Page is already checked Out. Please check in or discard checkout before activating features");
       }

       page.CheckOut();
       using (SPLimitedWebPartManager manager = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
       {
            string errorMessage = string.Empty;
            if (!web.IsRootWeb)
            {
               web = web.Site.RootWeb;
            }

            SPLimitedWebPartCollection webparts = manager.WebParts;
            foreach (System.Web.UI.WebControls.WebParts.WebPart webpart in webparts)
            {
                if (webpart.Title == webpartName)
                {
                  // Web Part found!
                  webpartExistance = true;
                  break;
                }                           
            }
            if (!webpartExistance)
            {
              //Creation of ContentEditor Webpart
              ContentEditorWebPart CEWP = new ContentEditorWebPart();
              CEWP.Title = webpartName;
              CEWP.Height = webpartHeight;
              CEWP.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;
              XmlElement ProjectDetailsTitle = new XmlDocument().CreateElement("content");
              ProjectDetailsTitle.InnerText = @"
                                              
"; CEWP.Content = ProjectDetailsTitle; manager.AddWebPart(CEWP, zone, zoneId); manager.SaveChanges(CEWP); } } page.CheckIn("Adding WebParts", SPCheckinType.MajorCheckIn); page.Publish("Publishing Page"); } else { throw new Exception("Page Not found for adding web parts"); } } catch (Exception ex) { throw ex; } finally { // This is to ensure next time when feature is activated if there is error in previous activation its taken care if (page != null && page.CheckOutType == SPFile.SPCheckOutType.Online) { page.UndoCheckOut(); } } }


In the above code snippet, i am checking whether the page exists or not, if exists then we are checking whether the page is already checked out or not. If page is checked out throwing an exception. Other wise using limited web part manager we are checking for whether the page have already have the web part with the same name. If the web part exists we are skipping, other wise we are adding content editor web part to the page and finally we are doing the check in and publishing the page.

Hope this post will be useful..

No comments:

Post a Comment