пятница, 2 июля 2010 г.

Content By Query Web Part content deployment.

Hi, everyone.



Not far a long time I have project where I developed site in the my own machine and then I exported from my site to customer. But I found some issues connecting with Content By Query Web Part.



When you moved your web part to another page site but as part of page created via user interface or SharePoint Designer, you will ensure that List Id property of web part was setted correctly. In your web part setted invalid List Id (old id of previos site), you have to update it.



I wrote chunk of code for you review, just updating in the one web part List Id.




private void UpdateListId(SPWeb web, String listName, String pageName)
{
SPListItem item = web.Lists["Pages"].Items.Cast().Where(t => t["FileRef"].ToString().Contains(pageName)).FirstOrDefault();
PublishingPage page = PublishingPage.GetPublishingPage(item);
page.CheckOut();
SPLimitedWebPartManager manager = item.File.GetLimitedWebPartManager(PersonalizationScope.Shared);
WebPart webPart = manager.WebParts.Cast().Where(w => w.GetType() == typeof(ContentByQueryWebPart)).FirstOrDefault();
if (webPart != null)
{
Console.WriteLine("Change ListId of web part from '{0}' to '{1}'",
((ContentByQueryWebPart) webPart).ListGuid,
web.Lists[listName].ID);
((ContentByQueryWebPart)webPart).ListGuid = web.Lists[listName].ID.ToString();
manager.SaveChanges(webPart);

Console.WriteLine("Save changes.");

page.Update();
page.CheckIn("Update list id of content by query webpart thats gathering timeline configuration items.");
item.File.Publish("Update list id of content by query webpart thats gathering timeline configuration items.");
item.File.Approve("Approve list id changes for content by query webpart thats gathering timeline configuration items.");
}
}


That's all :)