Search This Blog

Tuesday, March 25, 2008

Get User Collection from a Sharepoint Group

I have been working on getting a drop down list in Infopath to be populated with users of a Sharepoint Group. You can't connect directly to the drop drop list, so you will need to create 2 data sources, 1 to the web service and another xml file with dummy data which will be replace be information from the web service.
1. Create a data connection to the web service using the data connection wizard.
<http://servername/_vti_bin/UserGroup.asmx?WSDL>
Data Connection


2. Click next and select GetUserCollectionFromGroup
3. Enter Sample data, which is the name of the group which you wish to get data from.
4. You will need dummy data to be connected to the drop down list first. There are 2 dummy users in the xml, it tells infopath that it is repeating.

DummyData.xml

<?xml version=”1.0″ encoding=”utf-8″ ?>

<dfs:myFields xmlns:dfs=”http://schemas.microsoft.com/office/infopath/2003/dataFormSolution” xmlns:s0=”http://schemas.microsoft.com/sharepoint/soap/directory/” xmlns:my=”http://schemas.microsoft.com/office/infopath/2003/myXSD/2004-03-16T06-17-54″ xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema”> <dfs:queryFields> <s0:GetUserCollectionFromGroup> </s0:GetUserCollectionFromGroup> </dfs:queryFields> <dfs:dataFields> <GetUserCollectionFromGroupResponse xmlns=”http://schemas.microsoft.com/sharepoint/soap/directory/”> <GetUserCollectionFromGroupResult> <GetUserCollectionFromGroup> <Users> <User ID=”1″ Sid=”S-1-5-21-3593225548-2099380924-3969701235-500″ Name=”User1″ LoginName=”login1″ Email=”user1@users.com” Notes=”" IsSiteAdmin=”False” IsDomainGroup=”False”/> <User ID=”2″ Sid=”S-1-5-21-3593225548-2099380924-3969701235-1145″ Name=”User2″ LoginName=”login2″ Email=”user2@users.com” Notes=”" IsSiteAdmin=”False” IsDomainGroup=”False”/> </Users> </GetUserCollectionFromGroup> </GetUserCollectionFromGroupResult> </GetUserCollectionFromGroupResponse> </dfs:dataFields></dfs:myFields>
5. Save the dummydata.xml and connect it via the Data Connection Wizard as well.

6. Link the drop down list to the dummydata.xml
dropdownlist.png


7. You will need to go to the loading event of the infopath form, which you can get to by going to Tools>Programming>Loading Event.
It will open up Visual studio. Within the Loading Event, you will need the following piece of code.

public void FormEvents_Loading(object sender, LoadingEventArgs e){
//Dummy Data
//get an XPathNavigator to our webservice method
XPathNavigator dutyManagerDummy = this.DataSources["DutyManagerListDummyData"].CreateNavigator();
//create the XmlNamespaceManager that will allow us to navigate the xml
XmlNamespaceManager umanager = new XmlNamespaceManager(dutyManagerDummy.NameTable);
umanager.AddNamespace("dfs", http://schemas.microsoft.com/office/infopath/2003/dataFormSolution);
umanager.AddNamespace("tns", http://schemas.microsoft.com/sharepoint/soap/directory/);

//get the node that contains all the users from dummy data

XPathNavigator DutyManagerListDummyDataName = dutyManagerDummy.SelectSingleNode("/dfs:myFields/dfs:dataFields/tns:GetUserCollectionFromGroupResponse/tns:GetUserCollectionFromGroupResult/tns:GetUserCollectionFromGroup/tns:Users", umanager);
//Actual Data
//get an XPathNavigator to our webservice method
XPathNavigator dutyManager = this.DataSources["GetUserCollectionFromGroup"].CreateNavigator();
//create the XmlNamespaceManager that will allow us to navigate the xmlXmlNamespaceManager dmanager = new XmlNamespaceManager(dutyManager.NameTable);
dmanager.AddNamespace("dfs", http://schemas.microsoft.com/office/infopath/2003/dataFormSolution);
dmanager.AddNamespace("tns", http://schemas.microsoft.com/sharepoint/soap/directory/);
//get the node that contains all the users from the call to the web service
XPathNavigator dusers = dutyManager.SelectSingleNode("/dfs:myFields/dfs:dataFields/tns:GetUserCollectionFromGroupResponse/tns:GetUserCollectionFromGroupResult/tns:GetUserCollectionFromGroup/tns:Users", umanager);
try{
//replace the dummy information from the information from the web service
DutyManagerListDummyDataName.ReplaceSelf(dusers); }
catch(NullReferenceException ee){}
}
Run the preview and you should see the actual data instead of the dummy information.

No comments:

Post a Comment