I have created a community site programmatically and I want to add a web part to the home page using coding.
I want to add the web part above the red squared area.
How is it possible? Please help.
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");
file.CheckOut();
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;
xd.LoadXml(wikiField);
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);
//layoutsZoneInner.AppendChild(wpBoxDiv);
XmlAttribute attribute = xd.CreateAttribute("class");
wpBoxDiv.Attributes.Append(attribute);
attribute.Value = "ms-rtestate-read ms-rte-wpbox";
attribute = xd.CreateAttribute("contentEditable");
wpBoxDiv.Attributes.Append(attribute);
attribute.Value = "false";
// - div1
XmlElement div1 = xd.CreateElement("div");
wpBoxDiv.AppendChild(div1);
div1.IsEmpty = false;
attribute = xd.CreateAttribute("class");
div1.Attributes.Append(attribute);
attribute.Value = "ms-rtestate-read " + wpStorageKey.ToString("D");
attribute = xd.CreateAttribute("id");
div1.Attributes.Append(attribute);
attribute.Value = "div_" + wpStorageKey.ToString("D");
// - div2
XmlElement div2 = xd.CreateElement("div");
wpBoxDiv.AppendChild(div2);
div2.IsEmpty = false;
attribute = xd.CreateAttribute("style");
div2.Attributes.Append(attribute);
attribute.Value = "display:none";
attribute = xd.CreateAttribute("id");
div2.Attributes.Append(attribute);
attribute.Value = "vid_" + wpStorageKey.ToString("D");
item[SPBuiltInFieldId.WikiField] = xd.OuterXml;
item.Update();
file.CheckIn("");
file.Update();
web.Update();
}
Console.WriteLine("Completed..");
Console.ReadKey();
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
content
(in powershell) in between. But how to do that?
Commented
Dec 10, 2013 at 6:39