We have a requirement like whenever our one custom site is get created, on welcome page one of our custom web part should be added by default. Since our custom site is based on OOB team site template, welcome page of the site is wiki page.
So here I will discuss steps to add our custom web part on wiki page.
Here problem with Wiki page is we do not have web part zone so that we can’t directly add into given zone. Here on wiki page we have content field available and we need to put some mark up as the value of this field.
So written one web level feature and in feature activated added code
Steps:
- Getting the web instance.
- SPWeb web = properties.Feature.Parent as SPWeb;
- SPWeb web = properties.Feature.Parent as SPWeb;
- Get the welcome page.
- string welcomePageUrl = web.RootFolder.WelcomePage;
- SPFile welcomePage = web.GetFile(welcomePageUrl);
- Check out the welcome page.
- welcomePage.CheckOut(); // Here make sure that Check out type of welcome page //is none or checked out to current user then only check out the welcome page
- welcomePage.CheckOut(); // Here make sure that Check out type of welcome page //is none or checked out to current user then only check out the welcome page
- Get the webpart manager as.
- SPLimitedWebPartManager wpmgr = welcomePage.GetLimitedWebPartManager(PersonalizationScope.Shared) // Use Using //for this in actual code
- SPLimitedWebPartManager wpmgr = welcomePage.GetLimitedWebPartManager(PersonalizationScope.Shared) // Use Using //for this in actual code
- Create the instance of our custom webpart and add using webpartmanager.
- //Generate the id for the web part, this id is also used in markup which we
- //will assign content field
- Guid storageKey = Guid.NewGuid();
- string wpId = String.Format("g_{0}", storageKey.ToString().Replace('-', '_'));
- //My custom wp
- MyCustomWp myCustomWP = new MyCustomWp();
- myCustomWP.ID = wpId;
- myCustomWP.ChromeType = PartChromeType.None;
- myCustomWP.Title = “My Custom WP”;
- wpmgr.AddWebPart(MyWorkspacesWP, "wpz", 0);
- Set the MarkUp required to Content field for the wiki page.
- 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[] { storageKey.ToString("D") });
- //Get the list item to read the field
- SPListItem item = welcomePage.Item;
- //Wiki field
- item[SPBuiltInFieldId.WikiField] = markup;
- item.Update();
- welcomePage.Update();
- Finally check in the page.
- welcomePage.CheckIn(string.Empty, SPCheckinType.MajorCheckIn);
- welcomePage.CheckIn(string.Empty, SPCheckinType.MajorCheckIn);
Enjoy SharePoint
Your comments/suggestions/feedback is most welcome!

Miky GarinPosted Jul 11, 2018, 8:38 AM
Hi, I tried you exemple but my RichText stay emply. If I do the same with a console app it works. Any idea Why?
Gowtham RajamanickamPosted Mar 11, 2015, 1:20 PM
Good
Rambabu CHPosted Mar 4, 2015, 7:44 AM
Nice article