using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Collections;
namespace DelDocVer_Console
{
public class DelDocVersions
{
static string UserID = ConfigurationSettings.AppSettings["UserID"].ToString();
static string Password = ConfigurationSettings.AppSettings["Password"].ToString();
static string Domain = ConfigurationSettings.AppSettings["Domain"].ToString();
static string Site = ConfigurationSettings.AppSettings["Site"].ToString();
static spVersions.Versions VersionsService = new spVersions.Versions();
static void Main(string[] args)
{
if (args[0] != null)
{
ListService.Lists objService = new ListService.Lists();
objService.Timeout = 1500000;
WebCollService.Webs webObjService = new WebCollService.Webs();
webObjService.Timeout = 1500000;
webObjService.Url = Site + "/_vti_bin/webs.asmx";
webObjService.Credentials = new System.Net.NetworkCredential(UserID, Password, Domain);
XmlNode webCollNode = webObjService.GetAllSubWebCollection();
XName Root = "Webs";
XmlReader reader = XmlReader.Create(new StringReader(webCollNode.OuterXml));
var _webNodes = from c in XElement.Load(reader).Elements()
select new
{
SiteUrl = c.Attributes("Url").ToList()[0].Value
};
foreach (var _webUrl in _webNodes)
{
objService.Url = _webUrl.SiteUrl + "/_vti_bin/lists.asmx";
objService.Credentials = new System.Net.NetworkCredential(UserID, Password, Domain);
XmlNode listCollnode = objService.GetListCollection();
XmlReader Listreader = XmlReader.Create(new StringReader(listCollnode.OuterXml));
var _ListNodes = from c in XElement.Load(Listreader).Elements()
where c.Attributes("ServerTemplate").ToList()[0].Value == "101"
select new
{
ListName = c.Attributes("Title").ToList()[0].Value
};
foreach (var list in _ListNodes)
{
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
XmlElement query = xmlDoc.CreateElement("Query");
XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
viewFields.InnerXml = "";
queryOptions.InnerXml = "";
queryOptions.InnerXml = "
XmlNode nodeListItems =
objService.GetListItems(list.ListName, null, query, viewFields, "100000", queryOptions, null);
XmlReader Nodereader = XmlReader.Create(new StringReader(nodeListItems.OuterXml));
XName ItemVersions = "ows_EncodedAbsUrl";
var _encodedNode = from e in XElement.Load(Nodereader).Elements().Elements()
select new
{
EnodedURL = e.Attributes("ows_EncodedAbsUrl").ToList()[0].Value
};
//add version service and write the code.
VersionsService.Credentials = new System.Net.NetworkCredential(UserID, Password, Domain);
VersionsService.Url = _webUrl.SiteUrl + "/_vti_bin/Versions.asmx";
foreach (var version in _encodedNode)
{
XmlNode versions = VersionsService.GetVersions(version.EnodedURL);
DeleteVerions(versions, args[0], version.EnodedURL);
}
}
}
}
}
static void DeleteVerions(XmlNode versions, string args, string encodedURL)
{
try
{
XName xname = "version";
XmlReader Nodereader = XmlReader.Create(new StringReader(versions.OuterXml));
XmlElement versionsElement = (XmlElement)versions;
var versionCount = (from v in XElement.Load(Nodereader).Descendants()
where v.Attribute(xname) != null
select v).ToArray().OrderByDescending(a => !a.Attribute(xname).Value.Contains("@"));
int count = versionCount.Count();
foreach (var v in versionCount)
{
if (count > 5)
{
XmlNode Result = VersionsService.DeleteVersion(encodedURL, v.Attribute(xname).Value);
count--;
}
else
{
break;
}
}
}
catch(Exception ex)
{
}
}
}
}
No comments:
Post a Comment