How to add Promoted Links web part programmatically in SharePoint

Hi Friends, I would like to share some code help you to add promoted links web part in the home page using the client object model in SharePoint 2013/2016.

Adding a specific view of the web part is possible in the web part programmatically in SharePoint 2013/2016.

Add Promoted Links web part programmatically in SharePoint

Below are the steps to add the Promoted Links web part programmatically in SharePoint.

First Connect to site page -> Home Page of SharePoint 2013 site then Get web part Manager of the page.

And then Get List from current site, Get Specific view “Tiles” from that List.

Then Change list view to the web part presentation and Load webpart view to context, then Define webpart and webpart properties and Save webpart properties programmatically

Now, 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();
}
}

You may like the following SharePoint tutorials:

Here, we learned how to add the Promoted Links web part programmatically in SharePoint 2013/2016.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

jayabharathi

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

>