Sort People Picker Lists


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public XmlDocument LoadPersonnel()
  2. {
  3. string spURL = "http://localSharePointDev:80/";
  4. string thisWeb = "/";
  5.  
  6. //Collection Site collection using Spsite
  7. SPSite siteCollection = new SPSite(spURL);
  8. SPWeb thisSite = siteCollection.OpenWeb();
  9.  
  10. //Loop thru lists, build up response
  11. ArrayList targets = new ArrayList();
  12.  
  13. targets.Add("Personnel");
  14.  
  15.  
  16. XmlDocument returnDoc = new XmlDocument();
  17. XmlNode node = returnDoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
  18.  
  19. //add root element
  20. XmlElement rootElement = returnDoc.CreateElement("", "Root", "");
  21.  
  22. foreach (string listName in targets)
  23. {
  24. try
  25. {
  26. SPList secList = thisSite.Lists[listName];
  27. string match = "";
  28. SPQuery qry = new SPQuery();
  29.  
  30. SPListItemCollection items = secList.GetItems(qry);
  31.  
  32. //add root element
  33. XmlElement element = returnDoc.CreateElement("", listName.Replace(" ", ""), "");
  34. foreach (SPListItem item in items)
  35. {
  36. try
  37. {
  38. XmlElement person = returnDoc.CreateElement("", "Person", "");
  39.  
  40. SPFieldUserValueCollection userProp = ((SPFieldUserValueCollection)item["Person"]);
  41.  
  42. XmlElement txt = returnDoc.CreateElement("AccountId");
  43. txt.InnerText = userProp[0].User.LoginName;
  44. person.AppendChild(txt);
  45.  
  46. txt = returnDoc.CreateElement("Name");
  47. txt.InnerText = userProp[0].User.Name;
  48. person.AppendChild(txt);
  49.  
  50. element.AppendChild(person);
  51.  
  52. // Create XML for the multiple roles that may be present
  53. SPFieldLookupValueCollection userRoles = ((SPFieldLookupValueCollection)item["Role"]);
  54.  
  55. foreach (SPFieldLookupValue userRole in userRoles)
  56. {
  57. txt = returnDoc.CreateElement("Role");
  58. txt.InnerText = userRole.LookupValue;
  59. person.AppendChild(txt);
  60. }
  61.  
  62. element.AppendChild(person);
  63.  
  64. // Create XML for the multiple system areas that may be present
  65. SPFieldLookupValueCollection userSystemAreas = ((SPFieldLookupValueCollection)item["System Area"]);
  66.  
  67. foreach (SPFieldLookupValue userSystemArea in userSystemAreas)
  68. {
  69. txt = returnDoc.CreateElement("SystemArea");
  70. txt.InnerText = userSystemArea.LookupValue;
  71. person.AppendChild(txt);
  72. }
  73.  
  74. element.AppendChild(person);
  75. }
  76. catch (Exception ex)
  77. {
  78.  
  79. }
  80. }
  81. rootElement.AppendChild(element);
  82. items = null;
  83. qry = null;
  84. secList = null;
  85. }
  86. catch (Exception ex)
  87. {
  88.  
  89. }
  90. }
  91.  
  92. returnDoc.AppendChild(rootElement);
  93.  
  94. thisSite.Close();
  95. thisSite.Dispose();
  96. siteCollection.Close();
  97. siteCollection.Dispose();
  98.  
  99.  
  100. // Sort the XMLDocument by Person Name
  101. XmlDocument xmlDocCopy = new XmlDocument();
  102. xmlDocCopy.LoadXml(returnDoc.OuterXml);
  103. xmlDocCopy.SelectSingleNode("//Personnel").RemoveAll();
  104.  
  105. XmlNode node1 = returnDoc.SelectSingleNode("//Personnel");
  106. XPathNavigator navigator = node1.CreateNavigator();
  107. XPathExpression selectExpression = navigator.Compile("Person/Name");
  108. selectExpression.AddSort(".", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text);
  109. XPathNodeIterator nodeIterator = navigator.Select(selectExpression);
  110. while (nodeIterator.MoveNext())
  111. {
  112. XmlNode linkNode = returnDoc.SelectSingleNode("//Person[Name=\"" + nodeIterator.Current.Value + "\"]");
  113. XmlNode importedLinkNode = xmlDocCopy.ImportNode(linkNode, true);
  114. xmlDocCopy.SelectSingleNode("//Personnel").AppendChild(importedLinkNode);
  115. }
  116.  
  117. //xmlDocCopy.Save("c:\\temp\\Output.xml");
  118. return xmlDocCopy;
  119.  
  120. //return returnDoc;
  121. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.