none
Checkin/Checkout documents, Sharepoint, Web services

    Question

  • Hi guys,

    How can i check-in, check-out documents in sharepoint doc lib?
    I know it can be done with lists.asmx but can anyone guide me with code?\

    Thanks,
    Abhinay
    Friday, December 18, 2009 4:06 PM

Answers

  • Well you are going to have to be more specific in what you are trying to accomplish. The web service will only be able to checkin/checkout files that are checked out to the user calling the web service. You need to call GetListItems with a caml query for all the checked out items to the current user. Then take the returned xml and grab all the FileRef attributes. Then you can call CheckInFile on each one.  The code below will take the user's login name and query for any checked out documents. It returns the xmlNode and using xml linq extracts the FileRef attributes. It then takes the substring of the FileRef and appends the server name calling the UpdateListItemCheckInFile method wth the complete url to the document. You can the example code to pull document's based on whatever criteria you want and use the same technique to check out documents.

    public static void CheckInAllMyCheckedOutDocuments()
    {
                string query = "<mylistitemrequest><Query><Where><Eq><FieldRef Name=\"CheckoutUser\" /><Value Type=\"User\">basesmcdev2\\steve.curran</Value></Eq></Where><OrderBy><FieldRef Name=\"Title\"/></OrderBy></Query><ViewFields><FieldRef Name=\"Title\"/></ViewFields><QueryOptions><IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns></QueryOptions></mylistitemrequest>";
    
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(query);
    
    
                listservice.Lists listProxy = new listservice.Lists();
    
                listProxy.Url = "http://basesmcdev2/sites/tester1/_vti_bin/lists.asmx";
                listProxy.UseDefaultCredentials = true;
    
                XmlNode queryNode = doc.SelectSingleNode("//Query");
                XmlNode viewNode = doc.SelectSingleNode("//ViewFields");
                XmlNode optionNode = doc.SelectSingleNode("//QueryOptions");
    
    
                try
                {
                    XmlNode retNode = listProxy.GetListItems("tester2", null, queryNode, viewNode, string.Empty, optionNode, null);
    
                    XElement e = XElement.Parse(retNode.InnerXml);
                    var uniqueValues = (from t in e.Descendants().Attributes("ows_FileRef") select t.Value.ToString()).Distinct();
    
                    foreach (string fileRef in uniqueValues)
                    {
                        UpdateListItemCheckIn("http://basesmcdev2/" + fileRef.Substring(fileRef.IndexOf("#")+1));
                    }
    
    
    
                }
                catch (Exception ex)
                {
    
                    string msg = ex.Message;
                }
    
    
    }
    
    
    public static void UpdateListItemCheckIn(string fileRef)
    {
                listservice.Lists listProxy = new listservice.Lists();
    
    
                listProxy.Url = "http://basesmcdev2/sites/tester1/_vti_bin/lists.asmx";
                listProxy.UseDefaultCredentials = true;
    
                bool success = listProxy.CheckInFile(fileRef, string.Empty, "0");
    
    }
    
    
    

    certdev.com
    Friday, December 18, 2009 9:01 PM

All replies

  • The following code example shows how to use the lists web service to check out and then check in the file. Hope it helps.

    public static void UpdateListItemCheckOut()
    {
                listservice.Lists listProxy = new listservice.Lists();
    
    
                listProxy.Url = "http://basesmcdev2/sites/tester1/_vti_bin/lists.asmx";
                listProxy.UseDefaultCredentials = true;
                bool success = listProxy.CheckOutFile("http://basesmcdev2/sites/tester1/tester2/A0805051340568078224.txt", "False", string.Empty);
    
                success = listProxy.CheckInFile("http://basesmcdev2/sites/tester1/tester2/A0805051340568078224.txt", string.Empty, "0");
    
      
    }

    certdev.com
    Friday, December 18, 2009 5:42 PM
  • Thanks,
    But if i need to checkin/checkout all files in a doc lib then?
    Friday, December 18, 2009 6:17 PM
  • Well you are going to have to be more specific in what you are trying to accomplish. The web service will only be able to checkin/checkout files that are checked out to the user calling the web service. You need to call GetListItems with a caml query for all the checked out items to the current user. Then take the returned xml and grab all the FileRef attributes. Then you can call CheckInFile on each one.  The code below will take the user's login name and query for any checked out documents. It returns the xmlNode and using xml linq extracts the FileRef attributes. It then takes the substring of the FileRef and appends the server name calling the UpdateListItemCheckInFile method wth the complete url to the document. You can the example code to pull document's based on whatever criteria you want and use the same technique to check out documents.

    public static void CheckInAllMyCheckedOutDocuments()
    {
                string query = "<mylistitemrequest><Query><Where><Eq><FieldRef Name=\"CheckoutUser\" /><Value Type=\"User\">basesmcdev2\\steve.curran</Value></Eq></Where><OrderBy><FieldRef Name=\"Title\"/></OrderBy></Query><ViewFields><FieldRef Name=\"Title\"/></ViewFields><QueryOptions><IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns></QueryOptions></mylistitemrequest>";
    
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(query);
    
    
                listservice.Lists listProxy = new listservice.Lists();
    
                listProxy.Url = "http://basesmcdev2/sites/tester1/_vti_bin/lists.asmx";
                listProxy.UseDefaultCredentials = true;
    
                XmlNode queryNode = doc.SelectSingleNode("//Query");
                XmlNode viewNode = doc.SelectSingleNode("//ViewFields");
                XmlNode optionNode = doc.SelectSingleNode("//QueryOptions");
    
    
                try
                {
                    XmlNode retNode = listProxy.GetListItems("tester2", null, queryNode, viewNode, string.Empty, optionNode, null);
    
                    XElement e = XElement.Parse(retNode.InnerXml);
                    var uniqueValues = (from t in e.Descendants().Attributes("ows_FileRef") select t.Value.ToString()).Distinct();
    
                    foreach (string fileRef in uniqueValues)
                    {
                        UpdateListItemCheckIn("http://basesmcdev2/" + fileRef.Substring(fileRef.IndexOf("#")+1));
                    }
    
    
    
                }
                catch (Exception ex)
                {
    
                    string msg = ex.Message;
                }
    
    
    }
    
    
    public static void UpdateListItemCheckIn(string fileRef)
    {
                listservice.Lists listProxy = new listservice.Lists();
    
    
                listProxy.Url = "http://basesmcdev2/sites/tester1/_vti_bin/lists.asmx";
                listProxy.UseDefaultCredentials = true;
    
                bool success = listProxy.CheckInFile(fileRef, string.Empty, "0");
    
    }
    
    
    

    certdev.com
    Friday, December 18, 2009 9:01 PM