Thursday, March 22, 2012

SharePoint 2010 Form Based authentication for MS CRM 2011 Plugins

Some time you need to add, delete, or update custom items in a list on the SharePoint 2010 when plugins in MS CRM is fired up. This simple function can save your time :)

/// <summary>
/// Method for working with SharePoint lists
/// </summary>
/// <param name="authenticationWSAddress">URL for logging on to a SharePoint site</param>
/// <param name="listWSAddress">URL of SharePoint WebServices</param>
/// <param name="userName">String value, user name</param>
/// <param name="password">String value, password</param>
/// <param name="listName">String value, name of SharePoint list</param>
/// <param name="query">String value, batch query</param>
/// <returns></returns>
public string QueryForSharePoint(string authenticationWSAddress, string listWSAddress, string userName, string password, string listName, string query)
{
     string response;
     try
     {
        //Create an XmlDocument object and construct a Batch element and its attributes.
        XmlDocument xmlDoc = new XmlDocument();
        XmlElement elBatch = xmlDoc.CreateElement("Batch");
        elBatch.SetAttribute("OnError", "Continue");

        //Specify methods for the batch post using CAML.
        elBatch.InnerXml = query;

        Authentication spAuthentication = new Authentication();
        spAuthentication.Url = authenticationWSAddress;
        spAuthentication.CookieContainer = new CookieContainer();

        Lists spLists = new Lists {Url = listWSAddress};

        //Try to login to SharePoint site with Form based authentication
        LoginResult loginResult = spAuthentication.Login(userName, password);

        //If login is successfull
        if (loginResult.ErrorCode == LoginErrorCode.NoError)
        {
            //Get the cookie collection from the authenticatin web service
            CookieCollection cookies = spAuthentication.CookieContainer.GetCookies(new Uri(spAuthentication.Url));
            //Get the specific cookie which contains the security token
            Cookie cookie = cookies[loginResult.CookieName];
            //Initialize the cookie container of the list web service
            spLists.CookieContainer = new CookieContainer();
            //set the cookie of list web service to the authenticatio cookie
            if (cookie != null) spLists.CookieContainer.Add(cookie);

               //Update Lists in SharePoint 2010
                XmlNode responseNode = spLists.UpdateListItems(listName, elBatch);
                response = responseNode.OuterXml;
            }
            else
            {
                return loginResult.ErrorCode.ToString();
            }
        }
        catch (SoapException exp)
        {
            return string.Format("SOAP ERROR: {0}{1}", Environment.NewLine, exp.Message);
        }
        catch (Exception exp)
        {
            return string.Format("ERROR: {0}{1}", Environment.NewLine, exp.Message);
        }
            return response;
        }

How to use:
QueryForSharePoint(“http://spdemo/_vti_bin/Authentication.asmx”, http://spdemo/_vti_bin/Lists.asmx, “Admin”, “Password”, “List_Name”, string query);

P.S: Don’t forget to add a Web reference for “authenticationWSAddress” and “listWSAddress”.

No comments:

Post a Comment