Return to Snippet

Revision: 24583
at March 4, 2010 10:10 by blackf0rk


Initial Code
public XmlDocument LoadPersonnel()
        {
            string spURL = "http://localSharePointDev:80/";
            string thisWeb = "/";

            //Collection Site collection using Spsite
            SPSite siteCollection = new SPSite(spURL);
            SPWeb thisSite = siteCollection.OpenWeb();

            //Loop thru lists, build up response
            ArrayList targets = new ArrayList();

            targets.Add("Personnel");


            XmlDocument returnDoc = new XmlDocument();
            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 = "";
                    SPQuery qry = new SPQuery();

                    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
            XmlDocument xmlDocCopy = new XmlDocument();
            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;
        }

Initial URL


Initial Description


Initial Title
Sort People Picker Lists

Initial Tags
sort, directory, sharepoint

Initial Language
C#