I have created a community site programmatically and I want to add a web part to the home page using coding.

enter image description here

I want to add the web part above the red squared area.

How is it possible? Please help.

2 Answers 2


Got the solution by referring this and this posts, thanks a lot to them who posted.

My code is:

    SPFile file = web.GetFile("SitePages/Community%20Home.aspx");

    SPLimitedWebPartManager wpmgr = file.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
    Guid wpStorageKey = Guid.NewGuid();
    string webPartId = string.Format("g_{0}", wpStorageKey.ToString().Replace('-', '_'));

    ContentEditorWebPart cewp = new ContentEditorWebPart();
    cewp.ID = webPartId;
    cewp.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.TitleOnly;
    cewp.Title = "Custome Web Part";
    wpmgr.AddWebPart(cewp, "wpz", 1);

   // string markup = string.Format(CultureInfo.InvariantCulture, "<div class=\"ms-rtestate-read ms-rte-wpbox\" contentEditable=\"false\"><div class=\"ms-rtestate-read {0}\" id=\"div_{0}\"></div><div style='display:none' id=\"vid_{0}\"></div></div>", new object[] { wpStorageKey.ToString("D") });

    SPListItem item = file.Item;

    string wikiField = item[SPBuiltInFieldId.WikiField] as string;

    XmlDocument xd = new XmlDocument();
    xd.PreserveWhitespace = true;
    XmlElement layoutsTable = xd.SelectSingleNode("div/table") as XmlElement;
    XmlElement layoutsZoneInner
     = layoutsTable.SelectSingleNode("tbody/tr/td[2]/div/div") as XmlElement;

    //changing text
    XmlElement layoutsZoneText 
     = layoutsTable.SelectSingleNode("tbody/tr/td[1]/div/div/div") as XmlElement;
    layoutsZoneText.InnerText = "This is testing";

    // - wpBoxDiv
    XmlElement wpBoxDiv = xd.CreateElement("div");

    // getting first child [if you want to put at first otherwise comment the following line.
    XmlNode firstChild = layoutsZoneInner.FirstChild;

    layoutsZoneInner.InsertBefore(wpBoxDiv, firstChild);
    XmlAttribute attribute = xd.CreateAttribute("class");
    attribute.Value = "ms-rtestate-read ms-rte-wpbox";
    attribute = xd.CreateAttribute("contentEditable");
    attribute.Value = "false";
    // - div1
    XmlElement div1 = xd.CreateElement("div");
    div1.IsEmpty = false;
    attribute = xd.CreateAttribute("class");
    attribute.Value = "ms-rtestate-read " + wpStorageKey.ToString("D");
    attribute = xd.CreateAttribute("id");
    attribute.Value = "div_" + wpStorageKey.ToString("D");
    // - div2
    XmlElement div2 = xd.CreateElement("div");
    div2.IsEmpty = false;
    attribute = xd.CreateAttribute("style");
    attribute.Value = "display:none";
    attribute = xd.CreateAttribute("id");
    attribute.Value = "vid_" + wpStorageKey.ToString("D");

    item[SPBuiltInFieldId.WikiField] = xd.OuterXml;

This is console, you can add in event receiver or feature receiver as per your requirements.

Hope this helps others too.


We have done this using powershell for which i found a source on google.

In this they have stored webpart information in a config file and then checkout the page, then add webpart, then checkin and then approve

below is the Adding webpart to page with powershell

  • Thanks for replying sir, but there is no web part zone in the community zone page. Will it work? I don't have much concept about it.
    – users1100
    Dec 10, 2013 at 5:13
  • The script for adding a web part to a rich content field is similar to adding the web part to a traditional web part zone. The main difference is that we will add the web part to the hidden wpz zone and at the same time add the placeholder to the rich content field. We will also have to generate a unique ID for the web part in order to be able to set the reference to the web part in the HTML snippet inside the rich content. here is the link for your reference.
    – Amit
    Dec 10, 2013 at 6:27
  • Thank you for providing link, I did the same thing, but the existing content I have to preserve. The web part is added but the previous content is wiped out. How to preserve it?
    – users1100
    Dec 10, 2013 at 6:36
  • I think I need to load the file as xml and add the content(in powershell) in between. But how to do that?
    – users1100
    Dec 10, 2013 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.