Friday, March 8, 2013

Add contentEditor Web part to specific view


 private void AddContentEditorWebPartToAllNewsView(SPWeb oSite)        {            string webURL = oSite.Url;            string viewUrl = webURL + "/Pages/Forms/myView.aspx";            SPLimitedWebPartManager wpMgr = oSite.GetLimitedWebPartManager(viewUrl,                 PersonalizationScope.Shared);          
            bool wpExist = IsContentEditorWebPartExist(wpMgr);
            string error="Error on Import Content Editor Web Part";
            if (!wpExist)            {                string exportedWebPartXml = GetContentEditorHtmlSources();                XmlTextReader reader = new XmlTextReader(new System.IO.StringReader(exportedWebPartXml));
                System.Web.UI.WebControls.WebParts.WebPart importedWp = wpMgr.ImportWebPart(reader, out error);                wpMgr.AddWebPart(importedWp, "Main", 2);                wpMgr.SaveChanges(importedWp);            }        }
        private static bool IsContentEditorWebPartExist(SPLimitedWebPartManager wpMgr)        {            SPLimitedWebPartCollection wpColl = wpMgr.WebParts;            bool wpExist = false;
            for (int i = 0; i < wpColl.Count; i++)            {                if (wpMgr.WebParts[i].Title.ToLower().Equals("Content Editor".ToLower()))                {                    wpExist = true;                    break;                }            }            return wpExist;        }
   private string GetContentEditorHtmlSources()        {            StringBuilder builder = new StringBuilder();
            builder.Append("<WebPart xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns='http://schemas.microsoft.com/WebPart/v2'>");            builder.Append(" <Title>Content Editor</Title>");            builder.Append("<FrameType>Default</FrameType>");            builder.Append("<IsIncluded>true</IsIncluded>");            builder.Append("<ZoneID>Main</ZoneID>");            builder.Append("<PartOrder>2</PartOrder>");            builder.Append("<FrameState>Normal</FrameState>");            builder.Append(" <AllowRemove>true</AllowRemove>");            builder.Append("<AllowZoneChange>true</AllowZoneChange>");            builder.Append(" <AllowMinimize>true</AllowMinimize>");            builder.Append("<AllowConnect>true</AllowConnect>");            builder.Append("<AllowEdit>true</AllowEdit>");            builder.Append(" <AllowHide>true</AllowHide>");            builder.Append(" <IsVisible>true</IsVisible>");            builder.Append(" <HelpMode>Modeless</HelpMode>");            builder.Append(" <Dir>Default</Dir>");            builder.Append(" <MissingAssembly>Cannot import this Web Part.</MissingAssembly>");            builder.Append(" <PartImageLarge>/_layouts/images/mscontl.gif</PartImageLarge>");            builder.Append(" <Assembly>Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>");            builder.Append(" <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>");            builder.Append(" <ContentLink xmlns='http://schemas.microsoft.com/WebPart/v2/ContentEditor' />");            builder.Append(@" <Content xmlns='http://schemas.microsoft.com/WebPart/v2/ContentEditor'>    <![CDATA[<script type='text/javascript'>var theTDs = document.getElementsByTagName('td');var i=0;var TDContent = '';while (i < theTDs.length) {try {TDContent = theTDs[i].innerText || theTDs[i].textContent;if ((TDContent.indexOf('<DIV') == 0) && (TDContent.indexOf('</DIV>') >= 0)) {theTDs[i].innerHTML = TDContent;}}catch(err){}i=i+1;}</script>]]>  </Content>");
            builder.Append("<PartStorage xmlns='http://schemas.microsoft.com/WebPart/v2/ContentEditor' />");            builder.Append("</WebPart>");
            return builder.ToString();
        }

No comments:

Post a Comment