locked
Automatic checkin with meta-data RRS feed

  • Question

  • Hello,

    I am developing a plugin for SharePoint Online in javascript that would allow users to select and copy multiple documents across sites on the same server. I have finished most of the code but I hit a roadblock concerning documents checkin with new meta-data.

    A bit of background information first. The copy destination is always the same library, and that library has a custom mandatory column (site lookup column). This means that when a file is copied using the CopyIntoItemsLocal web service, it will always remain checked out until the required meta-data is added to the document. My program generates this meta-data, therefore it should be possible to automatically copy these documents. Or not?

    The problem:
    • Inserting meta-data is not possible on a checked out document, Lists.UpdateListItems returns a 0x81020016 "Document doesn't exist" error when trying.
    • Checking in a document with Lists.CheckInFile before adding the required meta-data isn't possible either. The lists web service provides a 500 Internal Server Error with the description: "You must fill out all required properties before checking in this document".
    What I tried:
    • Create a workflow with SharePoint Designer 2007 that checks in documents automatically when they are created. Apparently it doesn't work, even when an action automatically fills the required field of the item before the checkin operation. Maybe this is because the required column is a site column? I don't know.
    • Set the required column as optional. My application works in that case, but this isn't an option as that field always needs to be filled.

    I would really appreciate if someone could help me with this.

    Cheers,
    David D.
    Saturday, June 20, 2009 4:23 PM

Answers

  • If you need to set metadata when you are copying documents you can use the CopyIntoItems method of the copy web service. This would eliminate check outs due to required fields. You can read more here:

    http://www.sharepointblogs.com/smc750/archive/2009/05/19/uploading-content-into-sharepoint-let-me-count-the-ways.aspx


    If for some reason you don't want to do that you can create at timer job or batch program to grab the SPDocumentLibrary.CheckedOutFiles  collection and then call the TakeOverCheckOut method of the SPCheckedOutFile object, get the SPListItem, set the required field and then CheckIn.

     using (SPSite site = new SPSite("http://basesmcdev2/sites/tester1"))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists["tester2"];
    
                            foreach (SPCheckedOutFile scf in docLib.CheckedOutFiles)
                            {
    
                                scf.TakeOverCheckOut();
                                docItem = docLib.GetItemById(scf.ListItemId);
                                docItem.File.CheckIn(string.Empty);
                                docItem.File.Update();
    
                            }
    
                        }
    

    certdev.com
    Sunday, June 21, 2009 1:03 AM
  • Unfortunately, the CopyIntoItems will not put information into fields of "File", "Computed"  or "Lookup" types. The web service uses the SPCopy class's CopyIntoItem which makes a call to a private method called FieldShouldBeCopiedTo. This method contains the logic prventing Lookup fields from being copied. This web service original intent was to copy files from one site to another, and I suppose this logic prevents lookup field from being used since the "ID" used would not be the same between sites. So, you could still use a default value and then do an update using the lists web service, or run a batch job to undo the check outs and modify the value as suggested earlier, or use frontpage rpc call for creating the document. I tested using rpc and it has not problems setting the required value. RPC is just basically a http post. You can read more here:

    http://www.sharepointblogs.com/smc750/archive/2009/05/19/uploading-content-into-sharepoint-let-me-count-the-ways.aspx
    certdev.com
    Sunday, June 21, 2009 11:54 PM

All replies

  • If you need to set metadata when you are copying documents you can use the CopyIntoItems method of the copy web service. This would eliminate check outs due to required fields. You can read more here:

    http://www.sharepointblogs.com/smc750/archive/2009/05/19/uploading-content-into-sharepoint-let-me-count-the-ways.aspx


    If for some reason you don't want to do that you can create at timer job or batch program to grab the SPDocumentLibrary.CheckedOutFiles  collection and then call the TakeOverCheckOut method of the SPCheckedOutFile object, get the SPListItem, set the required field and then CheckIn.

     using (SPSite site = new SPSite("http://basesmcdev2/sites/tester1"))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists["tester2"];
    
                            foreach (SPCheckedOutFile scf in docLib.CheckedOutFiles)
                            {
    
                                scf.TakeOverCheckOut();
                                docItem = docLib.GetItemById(scf.ListItemId);
                                docItem.File.CheckIn(string.Empty);
                                docItem.File.Update();
    
                            }
    
                        }
    

    certdev.com
    Sunday, June 21, 2009 1:03 AM
  • Thank you for your answer Steve.

    I have tried the CopyIntoItems method by injecting the metadata I received from GetItem. What I did is add the required meta-data in a FieldInformation, then I added it next to all the others I received from the GetItem WS. This is what I used as a Fields parameter for the CopyIntoItems method.

    However, I still have some trouble. First I received an odd "String was not recognized as a valid DateTime" error, that I bypassed (for now) by changing the regional settings of the site from french to english (usa). This is strange because the DateTime fields have been mapped directly from the GetItem web service and thus haven't been touched.

    Now the document gets copied, but doesn't add the required lookup column meta-data, and therefore doesn't checkin. All the other meta-data gets copied successfully. Here is the FieldInformation I am trying to add (in XML), and that gets sent along with the other meta-data information fields:

    <FieldInformation Type = "Lookup" DisplayName = "Client" InternalName = "Client" Id = "3e437a58-c572-42aa-a2f7-59aef01c44cf" Value = "3;#(interne)"/>

    This FieldInformation element has the exact same content as if retrieved it from a checked in item with the GetItem WS. Any ideas on how to add that meta-data, and checkin the document?
    Sunday, June 21, 2009 5:08 PM
  • Unfortunately, the CopyIntoItems will not put information into fields of "File", "Computed"  or "Lookup" types. The web service uses the SPCopy class's CopyIntoItem which makes a call to a private method called FieldShouldBeCopiedTo. This method contains the logic prventing Lookup fields from being copied. This web service original intent was to copy files from one site to another, and I suppose this logic prevents lookup field from being used since the "ID" used would not be the same between sites. So, you could still use a default value and then do an update using the lists web service, or run a batch job to undo the check outs and modify the value as suggested earlier, or use frontpage rpc call for creating the document. I tested using rpc and it has not problems setting the required value. RPC is just basically a http post. You can read more here:

    http://www.sharepointblogs.com/smc750/archive/2009/05/19/uploading-content-into-sharepoint-let-me-count-the-ways.aspx
    certdev.com
    Sunday, June 21, 2009 11:54 PM