SharePoint Developer Center > SharePoint Products and Technologies Forums > SharePoint - Development and Programming > Upload a File into a DocLib and add metadata to it programatically
Ask a questionAsk a question
 

AnswerUpload a File into a DocLib and add metadata to it programatically

  • Wednesday, November 04, 2009 8:51 PMRachanaD Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello,

    I want to upload files to document library and also add 5-6 metadata field to it programatically .What are the ways I can do it?
    Are there any out of Box MOSS web service / Object model that will allow me to upload document as well as metadata?
    Also, I need to get the Document library ID back as response.

    Please suggest.

Answers

  • Thursday, November 05, 2009 1:12 AMSteve.CurranMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    You can use the copy web service (copy.asmx) to upload files to a document library and it will return you the url of the document. The same can be done with rpc. There is nothing out of the box that will return the document library id after uploading the document. However you can obtain the document library id from the returned url of the copy web service by using the SiteData web service's GetUrlSegments method. Below is some code to do this. It will also return the ListiItem id also.

       public static string GetListItemIDByUrl(string url)
       {
    
                sitedataservice.SiteData sd = new sitedataservice.SiteData();
                sd.Url = "http://basesmcdev2/sites/tester1/_vti_bin/sitedata.asmx";
                sd.UseDefaultCredentials = true;
    
    
                string webId = string.Empty;
                string listId = string.Empty;
                string itemId = string.Empty;
                string bucketId = string.Empty;
    
                bool ret = sd.GetURLSegments(url, out webId, out bucketId, out listId, out itemId);
    
                return itemId;
    
       }
    
    http://sharepointfieldnotes.blogspot.com/2009/09/uploading-content-into-sharepoint-let.html
    certdev.com
  • Thursday, November 05, 2009 5:58 PMSteve.CurranMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The url is returned in the last argument to the CopyIntoItems method. This is an array of CopyResult objects and each has a Url property. If you read the blog link the only performance constraint is the fact that it is soap and the binary is base64 encoded which expands the payload of the request. Otherwise, we use it here and it works very well. RPC also works very fast and scalable.
    certdev.com

All Replies

  • Thursday, November 05, 2009 1:12 AMSteve.CurranMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    You can use the copy web service (copy.asmx) to upload files to a document library and it will return you the url of the document. The same can be done with rpc. There is nothing out of the box that will return the document library id after uploading the document. However you can obtain the document library id from the returned url of the copy web service by using the SiteData web service's GetUrlSegments method. Below is some code to do this. It will also return the ListiItem id also.

       public static string GetListItemIDByUrl(string url)
       {
    
                sitedataservice.SiteData sd = new sitedataservice.SiteData();
                sd.Url = "http://basesmcdev2/sites/tester1/_vti_bin/sitedata.asmx";
                sd.UseDefaultCredentials = true;
    
    
                string webId = string.Empty;
                string listId = string.Empty;
                string itemId = string.Empty;
                string bucketId = string.Empty;
    
                bool ret = sd.GetURLSegments(url, out webId, out bucketId, out listId, out itemId);
    
                return itemId;
    
       }
    
    http://sharepointfieldnotes.blogspot.com/2009/09/uploading-content-into-sharepoint-let.html
    certdev.com
  • Thursday, November 05, 2009 2:18 PMRachanaD Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello Steve,

    Does the copy web service have any performance constraint?Also, How can get the URL or the ows_id field back after the upload is completed?
  • Thursday, November 05, 2009 5:58 PMSteve.CurranMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The url is returned in the last argument to the CopyIntoItems method. This is an array of CopyResult objects and each has a Url property. If you read the blog link the only performance constraint is the fact that it is soap and the binary is base64 encoded which expands the payload of the request. Otherwise, we use it here and it works very well. RPC also works very fast and scalable.
    certdev.com