Tuesday, 20 November 2012

Adding our custom web part on wiki page programmatically


 Hello Again, Today again new point J

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:
1.   Getting the web instance

SPWeb web = properties.Feature.Parent as SPWeb;

2.   Get the welcome page

string welcomePageUrl = web.RootFolder.WelcomePage;
SPFile welcomePage = web.GetFile(welcomePageUrl);

3.   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

4.   Get the webpart manager as

SPLimitedWebPartManager wpmgr = welcomePage.GetLimitedWebPartManager(PersonalizationScope.Shared) // Use Using //for this in actual code

5.   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 wpStorageKey = Guid.NewGuid();
       string webpartpId = String.Format("g_{0}"wpStorageKey.ToString().Replace('-', '_'));

//My custom wp
      MyCustomWp myCustomWP = new MyCustomWp();
      myCustomWP.ID = webpartpId;
      myCustomWP.ChromeType = PartChromeType.None;
      myCustomWP.Title = “My Custom WP”;

      wpmgr.AddWebPart(myCustomWP, "wpz", 0);

6.   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[] { wpStorageKey .ToString("D") });

       //Get the list item to read the field
SPListItem item = welcomePage.Item;
        
//Wiki field
item[SPBuiltInFieldId.WikiField] = markup;
       item.Update();
       welcomePage.Update();

7.   Finally check in the page

       welcomePage.CheckIn(string.Empty, SPCheckinType.MajorCheckIn);

Thanks! Enjoy SharePointJ

As usual your comments/suggestions/feedback is most welcome!

6 comments:

shabana said...

great post...very useful post.
Thanks..

Pieterjan Spoelders said...

hi upanyake. That's p. damn cool man. Many thanks for this :) very helpful

Evgeni Chakalov said...

Hello,

what if i want to add more than one web part. What is the procedure then.

Shirley Schmidt said...

Thanks for the post. I have just signed up for a free SharePoint 2013 site with http://www.cloudappsportal.com

Barbie Chiu said...

Thanks for sharing your article and for giving us the chance to read it. It is very helpful and encouraging. Visit my site too.

triciajoy.com

www.triciajoy.com

ufuk said...

@Evgeni Chakalov

if you want to add more than one webpart, set markups like this.


//Wiki field
item[SPBuiltInFieldId.WikiField] = markup + markup1 + markup2 + markup3;