public class SharePointExtensions
{
public static List<SPSiteInfo> getSiteSubSites(websProxy.Webs sharePointWebs)
{
try
{
XmlNode websResult = sharePointWebs.GetWebCollection();
XDocument results = XDocument.Parse(websResult.OuterXml);
XName name = XName.Get("Web", "http://schemas.microsoft.com/sharepoint/soap/");
var webs = from item in results.Descendants(name)
select new SPSiteInfo
(
item.Attribute("Title").Value,
item.Attribute("Url").Value
);
return webs.ToList();
}
catch (Exception ex)
{
throw ex;
}
}
public static List<SPListInfo> getSiteLists(listsProxy.Lists sharePointLists)
{
try
{
XmlNode listsResult = sharePointLists.GetListCollection();
XDocument results = XDocument.Parse(listsResult.OuterXml);
XName name = XName.Get("List", "http://schemas.microsoft.com/sharepoint/soap/");
var lists = from item in results.Descendants(name)
where item.Attribute("ServerTemplate").Value.ToString() ==
Properties.Settings.Default.SERVER_TEMPLATE
select new SPListInfo
(
new Guid(item.Attribute("ID").Value),
item.Attribute("Title").Value,
item.Attribute("DefaultViewUrl").Value
);
return lists.ToList();
}
catch (Exception ex)
{
throw ex;
}
}
public static List<SPFolderInfo> getListFolders(listsProxy.Lists sharePointLists,
string listName, string listText, string folderURL)
{
try
{
XmlDocument xmlDoc = new System.Xml.XmlDocument();
XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
XmlNode ndViewFields =
xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
XmlNode ndQueryOptions = null;
if (listName != folderURL)
{
ndQueryOptions = xmlDoc.CreateNode(
XmlNodeType.Element, "QueryOptions", "");
ndQueryOptions.InnerXml = string.Format(
"<Folder>{0}/{1}</Folder>", listName, folderURL);
}
XName name = XName.Get("data", "urn:schemas-microsoft-com:rowset");
XmlNode ndListItems =
sharePointLists.GetListItems(listText, null, ndQuery,
ndViewFields, null, ndQueryOptions, null);
XDocument results = XDocument.Parse(ndListItems.OuterXml);
var folders = from item in results.Descendants(name).Elements()
where item.Attribute("ows_ContentType").Value == "Carpeta"
select new SPFolderInfo
(
item.Attribute("ows_BaseName").Value,
item.Attribute("ows_ServerUrl").Value
);
return folders.ToList();
}
catch (Exception ex)
{
throw ex;
}
}
public static List<SPColumnInfo> getListColumns(listsProxy.Lists sharePointLists, string siteID)
{
try
{
List<SPColumnInfo> columns =new List<SPColumnInfo>();
XmlNode listsResult = sharePointLists.GetList(siteID);
XDocument results = XDocument.Parse(listsResult.OuterXml);
XName name = XName.Get("Field", "http://schemas.microsoft.com/sharepoint/soap/");
var lists = from item in results.Descendants(name)
select item;
foreach (var item in lists.ToList())
{
XAttribute aID = item.Attribute("ID");
XAttribute aType = item.Attribute("Type");
XAttribute aDisplayName = item.Attribute("DisplayName");
XAttribute aName = item.Attribute("Name");
XAttribute aHidden = item.Attribute("Hidden");
XAttribute aSealed = item.Attribute("Sealed");
XAttribute aReadOnly = item.Attribute("ReadOnly");
if (aID != null && aType != null && aDisplayName != null &&
aName != null)
{
bool ishidden = false;
bool issealed = false;
bool isreadonly = false;
if (aHidden != null && aHidden.Value.ToString() == "TRUE") ishidden = true;
if (aSealed != null && aSealed.Value.ToString() == "TRUE") issealed = true;
if (aReadOnly != null && aReadOnly.Value.ToString() == "TRUE") isreadonly = true;
columns.Add(new SPColumnInfo(
aID.Value.ToString(),
aType.Value.ToString(),
aDisplayName.Value.ToString(),
aName.Value.ToString(),
ishidden, issealed, isreadonly));
}
}
return columns;
}
catch (Exception ex)
{
throw ex;
}
}
public static void createMetadataColumns(listsProxy.Lists sharePointLists, string listID)
{
int createColumnsCount = 0;
if (!Properties.Settings.Default.OPTION_CREATE_METADATA_COLUMNS) return;
var cols = from c in SharePointExtensions.getListColumns(
sharePointLists, listID)
where c.Hidden == false && c.Sealed == false && c.ReadOnly == false
select c;
Dictionary<string, SPColumnInfo> columns = cols.ToDictionary(c => c.Name);
string newFieldsList = "";
if (!columns.ContainsKey(Properties.Settings.Default.COL_SUBJECT))
{
string subjectfield =
@"<Method ID='1'><Field Type='Text' DisplayName='{0}' MaxLength='255'/></Method>";
newFieldsList += string.Format(subjectfield, Properties.Settings.Default.COL_SUBJECT);
createColumnsCount++;
}
if (!columns.ContainsKey(Properties.Settings.Default.COL_TO))
{
string subjectfield =
@"<Method ID='2'><Field Type='Text' DisplayName='{0}' MaxLength='255'/></Method>";
newFieldsList += string.Format(subjectfield, Properties.Settings.Default.COL_TO);
createColumnsCount++;
}
if (!columns.ContainsKey(Properties.Settings.Default.COL_CC))
{
string subjectfield =
@"<Method ID='3'><Field Type='Text' DisplayName='{0}' MaxLength='255'/></Method>";
newFieldsList += string.Format(subjectfield, Properties.Settings.Default.COL_CC);
createColumnsCount++;
}
if (!columns.ContainsKey(Properties.Settings.Default.COL_BCC))
{
string subjectfield =
@"<Method ID='4'><Field Type='Text' DisplayName='{0}' MaxLength='255'/></Method>";
newFieldsList += string.Format(subjectfield, Properties.Settings.Default.COL_BCC);
createColumnsCount++;
}
if (!columns.ContainsKey(Properties.Settings.Default.COL_FROM))
{
string subjectfield =
@"<Method ID='5'><Field Type='Text' DisplayName='{0}' MaxLength='255'/></Method>";
newFieldsList += string.Format(subjectfield, Properties.Settings.Default.COL_FROM);
createColumnsCount++;
}
if (!columns.ContainsKey(Properties.Settings.Default.COL_SENT))
{
string subjectfield =
@"<Method ID='6'><Field Type='DateTime' DateOnly='FALSE' DisplayName='{0}'/></Method>";
newFieldsList += string.Format(subjectfield, Properties.Settings.Default.COL_SENT);
createColumnsCount++;
}
if (!columns.ContainsKey(Properties.Settings.Default.COL_SIZE))
{
string subjectfield =
@"<Method ID='7'><Field Type='Number' DisplayName='{0}'/></Method>";
newFieldsList += string.Format(subjectfield, Properties.Settings.Default.COL_SIZE);
createColumnsCount++;
}
if (!columns.ContainsKey(Properties.Settings.Default.COL_IMPORTANCE))
{
string subjectfield =
@"<Method ID='8'><Field Type='Choice' DisplayName='{0}'>;
<Default>{1}</Default>
<CHOICES>
<CHOICE>{2}</CHOICE>
<CHOICE>{3}</CHOICE>
<CHOICE>{4}</CHOICE>
</CHOICES>
</Field></Method>"
newFieldsList += string.Format(subjectfield,
Properties.Settings.Default.COL_IMPORTANCE,
Properties.Settings.Default.COL_IMPORTANCE_MEDIUM,
Properties.Settings.Default.COL_IMPORTANCE_HIGH,
Properties.Settings.Default.COL_IMPORTANCE_MEDIUM,
Properties.Settings.Default.COL_IMPORTANCE_LOW);
createColumnsCount++;
}
if (createColumnsCount > 0)
{
XmlNode ndList = sharePointLists.GetList(listID);
XmlNode ndVersion = ndList.Attributes["Version"];
XmlDocument xmlDoc = new System.Xml.XmlDocument();
XmlNode ndNewFields = xmlDoc.CreateNode(XmlNodeType.Element, "Fields", "");
ndNewFields.InnerXml = newFieldsList;
try
{
XmlNode ndReturn =
sharePointLists.UpdateList(listID,
null, ndNewFields, null, null,
ndVersion.Value);
}
catch (Exception ex)
{
throw ex;
}
}
}
}