/ Published in: C#
                    
                                        
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
public XmlDocument LoadPersonnel()
{
string spURL = "http://localSharePointDev:80/";
string thisWeb = "/";
//Collection Site collection using Spsite
SPWeb thisSite = siteCollection.OpenWeb();
//Loop thru lists, build up response
targets.Add("Personnel");
XmlNode node = returnDoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
//add root element
XmlElement rootElement = returnDoc.CreateElement("", "Root", "");
foreach (string listName in targets)
{
try
{
SPList secList = thisSite.Lists[listName];
string match = "";
SPListItemCollection items = secList.GetItems(qry);
//add root element
XmlElement element = returnDoc.CreateElement("", listName.Replace(" ", ""), "");
foreach (SPListItem item in items)
{
try
{
XmlElement person = returnDoc.CreateElement("", "Person", "");
SPFieldUserValueCollection userProp = ((SPFieldUserValueCollection)item["Person"]);
XmlElement txt = returnDoc.CreateElement("AccountId");
txt.InnerText = userProp[0].User.LoginName;
person.AppendChild(txt);
txt = returnDoc.CreateElement("Name");
txt.InnerText = userProp[0].User.Name;
person.AppendChild(txt);
element.AppendChild(person);
// Create XML for the multiple roles that may be present
SPFieldLookupValueCollection userRoles = ((SPFieldLookupValueCollection)item["Role"]);
foreach (SPFieldLookupValue userRole in userRoles)
{
txt = returnDoc.CreateElement("Role");
txt.InnerText = userRole.LookupValue;
person.AppendChild(txt);
}
element.AppendChild(person);
// Create XML for the multiple system areas that may be present
SPFieldLookupValueCollection userSystemAreas = ((SPFieldLookupValueCollection)item["System Area"]);
foreach (SPFieldLookupValue userSystemArea in userSystemAreas)
{
txt = returnDoc.CreateElement("SystemArea");
txt.InnerText = userSystemArea.LookupValue;
person.AppendChild(txt);
}
element.AppendChild(person);
}
catch (Exception ex)
{
}
}
rootElement.AppendChild(element);
items = null;
qry = null;
secList = null;
}
catch (Exception ex)
{
}
}
returnDoc.AppendChild(rootElement);
thisSite.Close();
thisSite.Dispose();
siteCollection.Close();
siteCollection.Dispose();
// Sort the XMLDocument by Person Name
xmlDocCopy.LoadXml(returnDoc.OuterXml);
xmlDocCopy.SelectSingleNode("//Personnel").RemoveAll();
XmlNode node1 = returnDoc.SelectSingleNode("//Personnel");
XPathNavigator navigator = node1.CreateNavigator();
XPathExpression selectExpression = navigator.Compile("Person/Name");
selectExpression.AddSort(".", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text);
XPathNodeIterator nodeIterator = navigator.Select(selectExpression);
while (nodeIterator.MoveNext())
{
XmlNode linkNode = returnDoc.SelectSingleNode("//Person[Name=\"" + nodeIterator.Current.Value + "\"]");
XmlNode importedLinkNode = xmlDocCopy.ImportNode(linkNode, true);
xmlDocCopy.SelectSingleNode("//Personnel").AppendChild(importedLinkNode);
}
//xmlDocCopy.Save("c:\\temp\\Output.xml");
return xmlDocCopy;
//return returnDoc;
}
Comments
 Subscribe to comments
                    Subscribe to comments
                
                