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;

    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";

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

Thanks in advance.

  • Can you try navWebPart.Update() before wpManager.SaveChanges(navWebPart); – Nadeem Yousuf-AIS Mar 11 '15 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.