Wednesday 31 December 2014

Add Custom Webppart to MySite Sharepoint -I

Recently I have requirement to modify the About Me page in My Site.I have create a site feature and in its event receiver added some code. Here is the solution if you have same requirement :

If you want to add Content Editor web part :

string Username = SPContext.Current.Web.CurrentUser.Name;
                    using (SPWeb web = siteCollection.OpenWeb())
                        if (web.WebTemplate == "SPSMSITEHOST")
                            //About Me Webpart
                            ContentEditorWebPart contentEditor = new ContentEditorWebPart();

                            XmlDocument xmlDoc = new XmlDocument();
                            XmlElement xmlElement = xmlDoc.CreateElement("HtmlContent");
                            xmlElement.InnerText = "<strong>About " + Username + "</strong>";
                            contentEditor.Content = xmlElement;
                            contentEditor.ChromeType = PartChromeType.None;
                            using (SPLimitedWebPartManager manager =
                              web.GetLimitedWebPartManager("Person.aspx", PersonalizationScope.Shared))
                                manager.AddWebPart(contentEditor, "TopZone", 2);

If you want to add custom web part to page:

No comments:

Post a Comment