How to retrieve a list of users from a SharePoint list within a K2 Destination Rule Code Block

Last post 07-02-2008, 11:55 AM by hyankov. 1 replies.
Sort Posts: Previous Next
  •  07-31-2007, 5:13 AM 17389

    How to retrieve a list of users from a SharePoint list within a K2 Destination Rule Code Block

     

    Overview

     

    SharePoint lists provide a very simply and quick way to maintain lists of data.  There are times where it would be useful to put fully qualified domain user IDs in a list and have these entries become used as destinations within a K2.net Activity. 

     

    In order to accomplish this, SharePoint must be called from within the K2.net Activity Destination Code Block.  This example utilizes one of the standard web services that ships SharePoint (Lists.asmx). 

     

    Please note, the SharePoint .NET Assembly object model could also potentially be used, however the object model can only be utilized on the same machine that SharePoint resides on.  If K2 and SharePoint are on different servers, the object model is not an option.

     

    Prerequisites

     

    -       K2.net 2003 Service Pack 4

    o   Earlier versions of K2.net would probably work, but this code has not been tested on them.

    -       Windows SharePoint Services ver 2 (aka SharePoint 2003).

     

    Details

     

    1. Create a list within SharePoint to house a list of Domain User Names

     

     

     

    1. Create new K2 Solution, Project and Process
      1. Note: sample code provided in C# only, so be sure to select C# as the Project language.
    2. Manually add a web reference to the K2 Project
      1. Right click on project in K2 Studio Solution Explorer
      2. Select ‘Properties’
      3. Select ‘References’
      4. Click the ‘Add’ button
      5. Click the ‘Web’ tab
      6. Enter URL to the SharePoint Lists web service

     For example: http://k2vpc:82/sites/SPS/_vti_bin/Lists.asmx

     

      1. Click ‘Discover’ button

     

     

      1. Click the ‘Select’ button

                                                                  i.      You should now see an something similar to below:

     

                                            ii.    Click the ‘OK’ button

                                           iii.    Add a .NET reference (from the .NET tab) to .NET assembly “System.Web.Services”

    1.    this should be in the list box

                                           iv.    Click ‘OK’ again at the project properties window

    1.    You should now be back at the empty process canvas

    1. Drag out a ‘Default’ activity
    2. Drag out a ‘Default Client Event’ and place it in the activity
    3. Connect the ‘Start’ box with the ‘Default’ activity.
    4. The process should look similar to:

     

    1. Right click on the ‘Default’ activity and select ‘Properties’
    2. Select ‘Destination Rule’
    3. Select ‘User Code’ option
    4. Click ‘Edit Code’ button
    5. Copy the below code sample into the empty destination rule code block.  Ensure that line breaks copy correctly to K2 Studio.

     

    Please note, you will need to to change the settings specific to your SharePoint environment and list contained in the variables “strURL”, “strListName” and “strColumnName”.  For the purposes of this demo, these values have been hardcoded, however Best Practice procedure would most likely retrieve these values from K2 Process Data fields or String Table entries. 

     

     

    public void Main(ref DestinationRuleContext K2)

    {

          K2.ResolveQueuesToUsers = true;

     

          // settings for this SharePoint list

          string strURL = "http://k2vpc:82/sites/SPS";

          string strListName = "Users";

          string strColumnName = "UserID";

     

          GetUsersFromList(K2, strURL, strListName, strColumnName);

    }

     

    private void GetUsersFromList(DestinationRuleContext K2, string strURL, string strListName, string strColumnName)

    {

          // instantiate the proxy webservice class

          Lists listService = new Lists();

     

          // make sure we're hitting the correct site

          listService.Url = strURL + "/_vti_bin/Lists.asmx";

     

          // use the current credentials

          listService.Credentials= System.Net.CredentialCache.DefaultCredentials;

     

          // load up a namespace manager with the namespaces we'll need for the XPaths

          System.Xml.XmlNamespaceManager oMgr = new System.Xml.XmlNamespaceManager(new System.Xml.NameTable());

          oMgr.AddNamespace( "sps", "http://schemas.microsoft.com/sharepoint/soap/" );

          oMgr.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");

          oMgr.AddNamespace("z", "#RowsetSchema");

               

          // retrieve a list of all the current lists on this virtual server

          System.Xml.XmlNode ndLists = listService.GetListCollection();

          // get the node containing the specific list we're interested in

          System.Xml.XmlNode ndList = ndLists.SelectSingleNode("//sps:List[@Title='" + strListName + "']", oMgr);

          if(ndList != null)

          {

                // recover the GUID for the list

                string strListGUID = ndList.Attributes["ID"].InnerText;

     

                // recover the schema for the list column

                System.Xml.XmlNode ndListDefinition = listService.GetList(strListGUID);

                // retrieve the definition for this column.  the display name is not always what the real

                // name is under the covers, so get the "real name"

                System.Xml.XmlNode ndField = ndListDefinition.SelectSingleNode("//sps:Fields/sps:Field[@DisplayName='" + strColumnName  + "']", oMgr);

                if(ndField != null)

                {

                      // recover the real name for this display name

                      string strRealName = ndField.Attributes["Name"].InnerText;

                                 

                      // setup a filtered view on this column name

                      System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();

                      System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");

                      viewFields.InnerXml = "<FieldRef Name='" + strRealName + "' />";

     

                      // Declare an XmlNode object and initialize it with the XML response from the GetListItems method.

                      System.Xml.XmlNode nodeTmpListItems = listService.GetListItems(strListGUID, "", null, viewFields, "", null);

     

                      // return all the rows (i.e. records in this list)

                      System.Xml.XmlNodeList ndRecs = nodeTmpListItems.SelectNodes("rs:data/z:row", oMgr);

                      // iterate the rows and recover the user id

                      foreach (System.Xml.XmlNode rec in ndRecs)

                      {

                            // build out the real attribute name

                            string strAttrName = "ows_" + strRealName;

                            // recover the value from this attribute

                            string strUserID = rec.Attributes[strAttrName].InnerText;

                            // set the K2 destination

                            Console.WriteLine("**** USER: " + strUserID);

                            K2.Destinations.Add(DestinationType.User, strUserID);

                      }

                }

          }

     

          return;

    }

     

     

     

     

     

     

    1. Create an Export Server within the K2 Project Properties
    2. Save all files in K2 Studio
    3. Export the process
    4. Grant a user permission to start and view this process in K2 Service Manager
    5. If necessary, create an application to start a process instance
    6. Check K2 Workspace to see if the tasks where assigned to the members in this SharePoint list.

     


    The statements and opinions made in my postings are my own, and do not reflect the opinions of SourceCode Technology Holdings, Inc. or its subsidiaries. All information is provided as is with no warranties, express or implied, and grants no rights or licenses.
  •  07-02-2008, 11:55 AM 24625 in reply to 17389

    Re: How to retrieve a list of users from a SharePoint list within a K2 Destination Rule Code Block

    Thanks, that works great. Except that I have to comment out the part where the URL is overriden. Even though I am setting the same URL as the reference, I get a 404 error when I overwrite the Url property...

    Hristo Yankov
View as RSS news feed in XML