question

sravyashivapuram-6930 avatar image
0 Votes"
sravyashivapuram-6930 asked sravyashivapuram-6930 commented

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

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

office-sharepoint-onlinesharepoint-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TongZhangMSFT-7548 avatar image
0 Votes"
TongZhangMSFT-7548 answered sravyashivapuram-6930 commented

Hi @sravyashivapuram-6930,

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.





image.png (11.4 KiB)
image.png (2.5 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @TongZhangMSFT-7548 ,

Thank you for your response. It worked.:) But the web part was added at the end of the homepage ( Since there are already existing web parts on the page). Is there a way I can specify it to be on the top so that it is visible to the users when they are in that site? Please advise.

Also, I need to add 'mailto:abc@xyz.com' functionality in the content editor text. Please let me know how can I do this. ( My text should look something like this - Ex: 'This is a first paragraph. To see more info, please contact us HERE' where HERE is a hyperlink that will open up the abc@xyz.com in outlook.

Appreciate all your time and help.

Thanks
SLS

0 Votes 0 ·
TongZhangMSFT-7548 avatar image TongZhangMSFT-7548 sravyashivapuram-6930 ·

Hi @sravyashivapuram-6930,

Based on my research and testing, I'm unfortunately to tell you that I have not found a way to specify webpart to the top of the page using CSOM. As a workaround, you can add the content editor webpart to a new page first, and then add other webparts in the page.

I have updated my answer , you can use the script in the answer to change the xmlWebPart , to achieve the "Here" is a hyperlink that will open up the abc@xyz.com in outlook.

1 Vote 1 ·

Thank you @TongZhangMSFT-7548 for your response. The hyperlink worked as expected.

0 Votes 0 ·