Checked Out Resources

Checked Out Resources

  • Comments 0

Phil Smail has this great code snippet for providing better error handling with regards to checked out resources:

While working with the PSI I wanted to check out a number of resources. Obviously the situation can arise where some of those resources are already checked out and I wanted to report that information to the client.

Unfortunately the PSClientError object only gives you information that there were resources already checked out but not which ones were checked out. The data is contained with the XML for the Exception so I wrote a helper function to extract this information out

The output from the code looks like the following:

The code is as follows:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Web.Services.Protocols;
using System.Xml;

namespace GetError
{
    class Program
    {
        private const string RESOURCESWEBSERVICE = "/_vti_bin/PSI/Resource.asmx";
        static GetError.ResourceWsc.Resource Resource = new GetError.ResourceWsc.Resource();


        static void Main(string[] args)
        {
            String PWAURL = args[0];
            Resource.Url = PWAURL + RESOURCESWEBSERVICE;
            Resource.Credentials = CredentialCache.DefaultCredentials;
            Guid[] restypeGuids = new Guid[2];
            //Change these GUIDS to res Guids in your system
            restypeGuids[0] = new Guid("{f461c3f7-e0c9-414f-ae1a-41abe862f709}");
            restypeGuids[1] = new Guid("{4e5d7702-7748-4a18-bc5d-7beef7004188}");

            Console.WriteLine("Trying to check out resources");

            try
            {
                Resource.CheckOutResources(restypeGuids);
            }
            catch (SoapException ex)
            {
                String tempxml = ex.Detail.InnerXml;
                ExtractError(tempxml);
            }
            Console.ReadLine();
        }

        static void ExtractError(String xmlstr)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlstr);
            foreach (XmlNode node in doc.DocumentElement.FirstChild.ChildNodes)
            {
                String ErrString = "";
                if(node.Name.Equals("item"))
                {
                    //We know its one error
                    XmlNode n = node.Attributes.GetNamedItem("value");
                    ErrString = "Error: Resource - " + n.Value;
                    XmlNode childn = node.FirstChild;
                    if (childn.Name.Equals("error"))
                    {
                        XmlNode no = childn.Attributes.GetNamedItem("name");

                        switch (no.Value)
                        {
                        case "CICOAlreadyCheckedOutToYou":
                            ErrString += " Resource already checked out to you";
                            break;
                        case "CICOCheckedOutInOtherSession":
                            ErrString += " Resource checked out in another session";
                            break;
                        case "CICOCheckedOutToOtherUser":
                            ErrString += " Resource already checked out to another user";
                            break;
                        }      
                    }
                }
                Console.WriteLine(ErrString);
            }
        }
    }
}

Leave a Comment
  • Please add 8 and 2 and type the answer here:
  • Post