How to add a content editor web part to a SharePoint site using CSOM

sravya shivapuram 211 Reputation points
2022-05-17T22:01:55.123+00:00

Hi,

I have a requirement to add a content editor web part to SharePoint online sites using CSOM. This content editor will simply contain text and need this text to be in red color. Please let me know how can I accomplish my requirement. Any help is greatly appreciated.

Thank you in advance.

Regards
Sravya

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,622 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,668 questions
0 comments No comments
{count} votes

Accepted answer
  1. Tong Zhang_MSFT 9,116 Reputation points
    2022-05-18T06:25:29.477+00:00

    Hi @sravya shivapuram ,

    Based on my my research and testing ,you can following the code to add a content editor web part to a SharePoint site using CSOM:

    using System;  
    using Microsoft.SharePoint.Client;  
    using Microsoft.SharePoint.Client.WebParts;  
    using System.Security;  
    namespace webpartadd  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                string userName = "xxxx@xxxxxxx.onmicrosoft.com";  
                string password = "xxxxxx";  
                string siteUrl = "https://xxxxxxx.sharepoint.com/sites/xxxxx";  
                SecureString securePassword = new SecureString();  
                foreach (char c in password)  
                {  
                    securePassword.AppendChar(c);  
                }  
                var credentials = new SharePointOnlineCredentials(userName, securePassword);  
                using (ClientContext clientContext = new ClientContext(siteUrl))  
                {  
                    try  
                    {  
                        clientContext.Credentials = credentials;  
                        clientContext.Load(clientContext.Web);  
                        clientContext.ExecuteQuery();  
                        File oFile = clientContext.Web.GetFileByServerRelativeUrl("/sites/xxxxx/SitePages/zellatest2.aspx");  
                        LimitedWebPartManager limitedWebPartManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared);  
                        string xmlWebPart = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<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\">" + "<Title>My Web Part</Title><FrameType>None</FrameType>" + "<Description>Use for formatted text, tables, and images.</Description>" + "<IsIncluded>true</IsIncluded><ZoneID></ZoneID><PartOrder>0</PartOrder>" + "<FrameState>Normal</FrameState><Height /><Width /><AllowRemove>true</AllowRemove>" + "<AllowZoneChange>true</AllowZoneChange><AllowMinimize>true</AllowMinimize>" + "<AllowConnect>true</AllowConnect><AllowEdit>true</AllowEdit>" + "<AllowHide>true</AllowHide><IsVisible>true</IsVisible><DetailLink /><HelpLink />" + "<HelpMode>Modeless</HelpMode><Dir>Default</Dir><PartImageSmall />" + "<MissingAssembly>Cannot import this Web Part.</MissingAssembly>" + "<PartImageLarge>/_layouts/images/mscontl.gif</PartImageLarge><IsIncludedFilter />" + "<Assembly>Microsoft.SharePoint, Version=13.0.0.0, Culture=neutral, " + "PublicKeyToken=94de0004b6e3fcc5</Assembly>" + "<TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>" + "<ContentLink xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" />" + "<Content xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\">" + "<![CDATA[<div style='color:red'>This is a first paragraph!</DIV> ]]></Content>" + "<PartStorage xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" /></WebPart>";  
                        WebPartDefinition oWebPartDefinition = limitedWebPartManager.ImportWebPart(xmlWebPart);  
                        limitedWebPartManager.AddWebPart(oWebPartDefinition.WebPart, "Left", 1);  
                        clientContext.ExecuteQuery();  
                    }  
                    catch (Exception e)  
                    {  
                        Console.WriteLine(e);  
                    }  
                }  
                Console.WriteLine("Succcesfully added webpart");  
            }  
        }  
    }  
    

    My test result:
    203027-image.png

    More information for reference:
    https://www.c-sharpcorner.com/blogs/add-update-and-remove-web-part-using-csom

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.

    ---------------------------------------------------------------------
    update---------------------------------------------------------------------

    Please use the following script to change the xmlWebPart , and you will achieve the "Here" is a hyperlink that will open up the abc@xyz .com in outlook.

                        string xmlWebPart = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<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\">" + "<Title>My Web Part</Title><FrameType>None</FrameType>" + "<Description>Use for formatted text, tables, and images.</Description>" + "<IsIncluded>true</IsIncluded><ZoneID></ZoneID><PartOrder>0</PartOrder>" + "<FrameState>Normal</FrameState><Height /><Width /><AllowRemove>true</AllowRemove>" + "<AllowZoneChange>true</AllowZoneChange><AllowMinimize>true</AllowMinimize>" + "<AllowConnect>true</AllowConnect><AllowEdit>true</AllowEdit>" + "<AllowHide>true</AllowHide><IsVisible>true</IsVisible><DetailLink /><HelpLink />" + "<HelpMode>Modeless</HelpMode><Dir>Default</Dir><PartImageSmall />" + "<MissingAssembly>Cannot import this Web Part.</MissingAssembly>" + "<PartImageLarge>/_layouts/images/mscontl.gif</PartImageLarge><IsIncludedFilter />" + "<Assembly>Microsoft.SharePoint, Version=13.0.0.0, Culture=neutral, " + "PublicKeyToken=94de0004b6e3fcc5</Assembly>" + "<TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>" + "<ContentLink xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" />" + "<Content xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\">" + "<![CDATA[<div style='color:red'>This is a first paragraph. To see more info, please contact us  <a href='mailto:abc@xyz.com?subject=your title&body=TThe message'>Here</a></DIV> ]]></Content>" + "<PartStorage xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" /></WebPart>";  
    

    My test result:
    203569-image.png


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



0 additional answers

Sort by: Most helpful