Wednesday, 4 November 2009

Reset All sites to site definition (gosted) Programatically

If you like me need to reset your migrated page to site definition you can:

1) bother yourself with using SharePoint Designer and change the customized page to uncostomized...one by one.

2) go to the site and under site setting click on the "Reset to site definition" where you will be able to reset a site or all the site under a site collection.

3) if you want a quick and reusable solution, create some code using web service model and let the Sharepoint object model work for you. Create a winForm with 3 textbox (txtSiteUrl,txtUsername,txtPassword) and 1 button. Add a web reference (Wss3).Then add the following code

Here is the code:

private void StartProcess()
{
// Declare and initialize a variable for the Lists Web Service.
Wss3.Webs WebsService = new Wss3.Webs();

if (txtSiteUrl.Text != "")
WebsService.Url = txtSiteUrl.Text + "/_vti_bin/Webs.asmx";
try
{
WebsService.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtPassword.Text, "DOMAIN_NAME_OF_THE_USER");

/* Declare an XmlNode object and initialize it with the XML response from
the GetListItems method.*/

System.Xml.XmlNode nodeListItems = WebsService.GetWebCollection();

System.Xml.XmlNamespaceManager mgr = new System.Xml.XmlNamespaceManager(nodeListItems.OwnerDocument.NameTable);
mgr.AddNamespace("wb", "http://schemas.microsoft.com/sharepoint/soap/");

System.Xml.XmlNodeList nodes = nodeListItems.SelectNodes("//wb:Web", mgr);

foreach (System.Xml.XmlNode node in nodes)
{
string url = node.Attributes["Url"].Value;
ResetToSiteDefition(url);
}

MessageBox.Show("Validation Completed");
}
catch (System.Net.WebException webException)
{
MessageBox.Show("Please, Verify the Credential", "Error StartProcess", MessageBoxButtons.OK, MessageBoxIcon.Error);

}
catch (SoapServerException soapException)
{
MessageBox.Show("Please, Verify the Url", "Error StartProcess", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void ResetToSiteDefition(string url)
{
Wss3.Webs WebsService = new Wss3.Webs();

try
{
WebsService.Credentials = new System.Net.NetworkCredential(txtUsername.Text, txtPassword.Text, "DOMAIN_NAME_OF_THE_USER");


if (!string.IsNullOrEmpty(url))
{
WebsService.Url = string.Format("{0}/_vti_bin/Webs.asmx", url);
WebsService.RevertAllFileContentStreams();
}
}
catch (System.Net.WebException webException)
{
MessageBox.Show("Please, Verify the Credential", "Error StartProcess", MessageBoxButtons.OK, MessageBoxIcon.Error);

}
catch (SoapServerException soapException)
{
MessageBox.Show("Please, Verify the Url", "Error StartProcess", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

No comments:

Post a Comment