mercredi 11 mars 2015

Set view fields in content query web part programmatically



I am using the code below to update a web part on a home page, it sets the correct settings but when I go to the page with the web part it doesn't pick up the links from the Navigation list.


What I have to do is to Edit page -> Edit web part -> just click OK and the links are visible - don't have to change anything, why do I have to do this, what am I missing?



PublishingPageCollection publishingPages = PublishingWeb.GetPublishingWeb(web).GetPublishingPages();
PublishingPage page = publishingPages[0];

SPFile file = page.ListItem.File;
file.CheckOut();

using (SPLimitedWebPartManager wpManager = web.GetLimitedWebPartManager(file.ServerRelativeUrl, PersonalizationScope.Shared))
{
wpManager.Web.AllowUnsafeUpdates = true;

foreach (WebPartPages.WebPart wp in wpManager.WebParts.OfType<ContentByQueryWebPart>())
{
if (wp.DisplayTitle == "Navigation")
{
var navWebPart = wp as ContentByQueryWebPart;
var navList = web.Lists["Navigation"];
navWebPart.WebUrl = navList.ParentWeb.ServerRelativeUrl;
navWebPart.ListGuid = navList.ID.ToString();
navWebPart.ListName = navList.Title;
navWebPart.CommonViewFields = "URL,Text";
wpManager.SaveChanges(navWebPart);
break;
}
}
}

file.Update();
file.CheckIn("web parts added");
file.Publish("Auto publish");


Thanks in advance.








0 commentaires:

Enregistrer un commentaire