Monday 24 February 2014

Programmatically Adding a Content Editor Web Part

Following code snippet can be used to add a web part in an existing SharePoint website:


  private void AddWebPart()
        {
            const int zoneIndex = 0;
            var webPartGalleryAccessor = new WebPartGalleryAccessor();
            var contentEditor = new ContentEditorWebPart
            {
                ZoneID = "ImageLeftZone",
                Title = "Sample",
                Width = "500px",
                Height = "350px",
                ChromeState = System.Web.UI.WebControls.WebParts.PartChromeState.Normal,
                ChromeType =
                    System.Web.UI.WebControls.WebParts.PartChromeType.None
            };
            //Add content 
            XmlDocument xmlDoc = new XmlDocument();
            XmlElement xmlElement = xmlDoc.CreateElement("Root");
            xmlElement.InnerText =            
                   "<img src='/sites/testsite/_catalogs/masterpage/image/video-boximg.JPG' width='370' height='220' />";
            contentEditor.Content = xmlElement;
            contentEditor.Content.InnerText = xmlElement.InnerText;
            contentEditor.Title = "Products";
            webPartGalleryAccessor.AddWebPartToSitePage(_rootSiteUrl, _subSiteUrl, _pageUrl, contentEditor, zoneIndex);
        }

WebPartGalleryAccessor Class:

 public void AddWebPartToSitePage(string rootSiteUrl, string subSiteUrl, string pageUrl, WebPart webPart, int zoneIndex)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (var pageLoadSite = new SPSite(rootSiteUrl))
                    {
                        pageLoadSite.AllowUnsafeUpdates = true;                      
                        using (var pageLoadWeb = pageLoadSite.OpenWeb())
                        {
                            pageLoadWeb.AllowUnsafeUpdates = true;
                            AddWebPartToPage(pageLoadWeb, pageUrl, webPart, zoneIndex);
                            pageLoadWeb.AllowUnsafeUpdates = false;
                        }
                        pageLoadSite.AllowUnsafeUpdates = false;
                    }
                });

            }
            catch (Exception exception)
            {
                //throw new Exception("Unable to Add WebPart To Site Page: " + pageUrl + " Webpart title: " + webPart.Title + "Error: " + exception.Message);
            }
        }


private string AddWebPartToPage(SPWeb web, string pageUrl, WebPart webPart, int zoneIndex)
        {
            try
            {
                SPFile file = web.GetFile(pageUrl);
                if (file.CheckOutType == SPFile.SPCheckOutType.None)
                {
                    file.CheckOut();
                }

                using (SPLimitedWebPartManager manager = file.GetLimitedWebPartManager((PersonalizationScope)Microsoft.SharePoint.Client.WebParts.PersonalizationScope.Shared))
                {
                    manager.AddWebPart(webPart, webPart.ZoneID, zoneIndex);
                    file.CheckIn("Webpart Added.");
                    file.Publish("");
                    file.Approve("");
                    return webPart.ID;
                }
            }
            catch (Exception exception)
            {
                throw new Exception("Unable to Add WebPart To Page: " + pageUrl + " Webpart title: " + webPart.Title + "Error: " + exception.Message);
            }
        }

No comments:

Post a Comment