Wednesday, June 3, 2009

How can I modify "Modified", "Modified By", "Created" and "Created By" Field Values Without Changing Version in Published Page List?

SPSite site = null;

SPWeb web = null;
site = new SPSite("http://portal/");
web = site.OpenWeb();
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
SPContentTypeId myContentType = new SPContentTypeId("MyContentTypeId");
PageLayout[] layouts = publishingWeb.GetAvailablePageLayouts(myContentType );
PageLayout pageLayout = layouts[0];
string pageName = Guid.NewGuid().ToString() + ".aspx";
SPUser user = web.EnsureUser("DOMAIN\\ashishk");
string createdDate = "2009-06-03T22:35:10Z";
string modifiedDate = "2009-06-03T22:35:10Z";
web.AllowUnsafeUpdates = true;
PublishingPage newPage = publishingWeb.GetPublishingPages().Add(pageName, pageLayout);
newPage.ListItem["Author"] = user.ID;
newPage.ListItem["Editor"] = user.ID;
newPage.ListItem["Created"] = createdDate;
newPage.ListItem["Modified"] = modifiedDate;
newPage.ListItem.UpdateOverwriteVersion();
web.AllowUnsafeUpdates = false;
web.Dispose();
site.RootWeb.Dispose();
site.Dispose();

0 comments:

Post a Comment