We recently had a performance issue in our environment at client side and after some investigation we found out that there are many versions( Up to 500 No kidding) of documents(specially he ASPX) sit in the Content Database which are not even required.
Solution:
We developed a small .NET WinForm (MS Visual Studio 2005) based utility that uses SharePoint Webservice Versions.asmx to cleanup(delete all versions of an item and leave the latest one) the lists with non used versions.
Following is the Code to delete an item version (not the whole item):
1. Get the all the lists function:
//Class Level Variables
//You need to add the web reference to the project
public static string strVersionWebServiceURL;
WSLists.Lists _listService;
WSVersion.Versions _versionService;
// Actual Function
private void btnGetListCollection_Click(object sender, EventArgs e)
{
// List and Version web service declaration and initialization
strListWebServiceURL = txtSiteURL.Text + "/_vti_bin/lists.asmx";
strVersionWebServiceURL = txtSiteURL.Text + "/_vti_bin/versions.asmx";
System.Net.NetworkCredential nt = new System.Net.NetworkCredential(txtUserName.Text, txtPassword.Text);
_listService = new WSLists.Lists();
_listService.Credentials = nt;
_listService.Url = strListWebServiceURL;
_versionService = new WSVersion.Versions();
_versionService.Credentials = nt;
_versionService.Url = strVersionWebServiceURL;
// end declaration
if (txtSiteURL.Text != string.Empty && txtUserName.Text != string.Empty && txtPassword.Text != string.Empty)
{
lsListCollection.Items.Clear();
XmlNode _xmlListCollection = _listService.GetListCollection();
for (int i = 0; i < _xmlListCollection.ChildNodes.Count; i++)
{
string strListName = _xmlListCollection.ChildNodes[i].Attributes["Title"].Value.ToLower();
lsListCollection.Items.Add(strListName);
}// End For Loop
}
else
{
MessageBox.Show("Site URL, User Name and Password must be entered");
}
}
2. Delete all the versions of all the items from the selected list function:
private void btnDeleteAllVersionofList_Click(object sender, EventArgs e)
{
string strndQuery = "<OrderBy><FieldRef Name=\"Modified\" Ascending=\"FALSE\" /></OrderBy>";
/*string strndQuery = "<Where><Eq><FieldRef Name='ID' /><Value Type='Integer'>" + "76" + "</Value></Eq></Where>" +
"<OrderBy><FieldRef Name=\"Modified\" Ascending=\"FALSE\" /></OrderBy>";
**/
XmlNodeList _nodelist = GetListItems(strndQuery);
if (_nodelist != null)
{
for (int i = 0; i < _nodelist.Count; i++)
{
try
{
XmlNode _node = _nodelist[i];
string strItemURL = _node.Attributes["ows_EncodedAbsUrl"].Value;
// MessageBox.Show(strItemURL);
XmlNode xmlVersionCollection = _versionService.DeleteAllVersions(strItemURL);
}
catch {}
}
MessageBox.Show("Done!!!");
}
}
private XmlNodeList GetListItems(string strndQuery)
{
XmlNodeList _nodelist = null;
string strListName = lsListCollection.SelectedItem.ToString();
XmlDocument xmlDoc = new XmlDocument();
XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
XmlNode ndViewFields =
xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
XmlNode ndQueryOptions =
xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
ndQueryOptions.InnerXml =
"<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>" +
"<DateInUtc>TRUE</DateInUtc>" + "<ViewAttributes Scope='Recursive'/>";
ndQuery.InnerXml = strndQuery;
//add view fields to fix
ndViewFields.InnerXml =
"<FieldRef Name='Title' />" +
"<FieldRef Name='Abstract' />" +
"<FieldRef Name='ContentURL' />" +
"<FieldRef Name='LinkFilename' />" +
"<FieldRef Name='Last_x0020_Modified' />" +
"<FieldRef Name='DocIcon' />" +
"<FieldRef Name='DaysNew' />" +
"<FieldRef Name='RiskManagementLevel' />" +
"<FieldRef Name='File_x0020_Size' />" +
"<FieldRef Name='CategoryFilter_x002F_USDocumentType' />" +
"<FieldRef Name='ID' />" +
"<FieldRef Name='MediaType' />" +
"<FieldRef Name='EncodedAbsUrl' />" +
"<FieldRef Name='USPrimaryServiceLine_x002F_USPrimarySubService' />" +
"<FieldRef Name='USSecondaryServiceLine_x002F_USSecondarySubService' />" +
"<FieldRef Name='USIndustry_x002F_USSector' />" +
"<FieldRef Name='USServiceLine_x002F_ServiceOffering' /><FieldRef Name='ServerUrl' /><FieldRef Name='Modified' />";
try
{
XmlDocument _xmlDocument = new XmlDocument();
XmlNamespaceManager _xmlNameSpaceMngr = new XmlNamespaceManager(_xmlDocument.NameTable);
_xmlNameSpaceMngr.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
_xmlNameSpaceMngr.AddNamespace("z", "#RowsetSchema");
XmlNode ndListItems = null;
ndListItems =
_listService.GetListItems(strListName, null, ndQuery,
ndViewFields, "2500", ndQueryOptions, null);
_nodelist = ndListItems.SelectNodes("/rs:data/z:row", _xmlNameSpaceMngr);
}
catch
{
}
return _nodelist;
}
No comments:
Post a Comment