Add Promoted Links webpart in home page of SharePoint 2013 programmatically through apps

Check out Best Alternative to InfoPath


Hi Friends, I would like to share some code help you to add promoted links web part in homepage using client object model.


1. It is helpful when you go for SharePoint 2013 apps programming.

2. Adding specific view of web part is possible in web part programmatically in SP2013.

Steps:

1. Connect to site page->Home Page of SharePoint 2013 site

2. Get web part Manager of the page.

3. Get List from current site.

4. Get Specific view “Tiles” from that List.

5. Change list view to web part presentation

6. Load webpart view to context

7. Define webpart and webpart properties.

8. Save webpart properties programmatically

9. Load webpart into HomePage.



Here is the code :
//Get homepage url

String HPUrl = string.Format(“{0}/SitePages/{1}”, web.ServerRelativeUrl, Consts.NEWHOMEPAGENAME);

File page = web.GetFileByServerRelativeUrl(HPUrl);

//get the web part manager
LimitedWebPartManager wpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared);

//Add the webpart
Site s = ((ClientContext)(web.Context)).Site;

web.Context.Load(s);

web.Context.ExecuteQuery();

foreach (string promotedLinksTitle in Consts.PROMOTEDLINKSTITLES.Reverse())

{
//get promoted links list
List promotedlinks = web.Lists.GetByTitle(promotedLinksTitle);

web.Context.Load(promotedlinks);

web.Context.ExecuteQuery();

//Get Tiles view of the promoted links list
web.Context.Load(promotedlinks.Views);

web.Context.ExecuteQuery();

View tilesView = promotedlinks.Views.ToList().Find(p => p.Title == “Tiles”);

if (tilesView != null)
{
//Get page of the view
File pageTilesView = web.GetFileByServerRelativeUrl(tilesView.ServerRelativeUrl);

web.Context.Load(pageTilesView);

web.Context.ExecuteQuery();

//Get webpartManager of the page
LimitedWebPartManager wpmView = pageTilesView.GetLimitedWebPartManager(PersonalizationScope.Shared);

web.Context.Load(wpmView);

web.Context.ExecuteQuery();

//Get Tiles webpart
web.Context.Load(wpmView.WebParts);

web.Context.ExecuteQuery();

List<WebPartDefinition> viewWebPartDefs = wpmView.WebParts.ToList();

WebPartDefinition webpartDef = viewWebPartDefs.First();//Only one webpart definition is available

web.Context.Load(webpartDef.WebPart);

web.Context.ExecuteQuery();

//Add a copy of the webpart in Home Page
WebPartDefinition addedWebPartDef = wpm.AddWebPart(webpartDef.WebPart, “Body”, 0);

web.Context.ExecuteQuery();

//Open the webpart definition for changes
addedWebPartDef.OpenWebPart();

web.Context.ExecuteQuery();

//Load webpart
web.Context.Load(addedWebPartDef.WebPart);

web.Context.ExecuteQuery();

web.Context.Load(addedWebPartDef.WebPart.Properties);

web.Context.ExecuteQuery();

//Define hidden chrome type
addedWebPartDef.WebPart.Properties[“ChromeType”] = System.Web.UI.WebControls.WebParts.PartChromeType.None;

//Save changes of the webpart definition
addedWebPartDef.SaveWebPartChanges();

web.Context.ExecuteQuery();

//Close webpart definition previously opened
addedWebPartDef.CloseWebPart();

web.Context.ExecuteQuery();

}

}


Thanks for reading my article.


You May Also like the Following SharePoint Online Tutorials:

About jayabharathi

The turning point in the process of growing up is when you discover the core of strength within you that survives all hurt.

View all posts by jayabharathi →