الإجابة Updating Document Library through Copy.asmx

  • יום שישי 19 אוגוסט 2011 12:49
     
      קוד כלול

    Hi,

    I'm trying to update/add documents in a document library using the copy.asmx web service. I have problems in updating fields which are using lookups from other lists and external content types. The fields set as text or numbers are being updated correctly and the file is uploaded correctly too.

    So far, I have used the following code from what I have found on the internet and other forums:

    //Copy WebService Settings 
            string webUrl = "http://sharepointurl:7780/";
            WSCopy.Copy copyService = new WSCopy.Copy();
            
            copyService.Url = webUrl + "/_vti_bin/copy.asmx";
            copyService.Credentials = new NetworkCredential("Username", "Password", "Domain");
    
            //Declare and initiates the Copy WebService members for uploading 
            string sourceUrl = @"C:\Download\address2.jpg";
    
            //Change file name if not exist then create new one   
            string[] destinationUrl = { "http://sharepointurl:7780/accounting/testDamian2.jpg" };
    
            WSCopy.CopyResult cResult1 = new WSCopy.CopyResult();
            WSCopy.CopyResult cResult2 = new WSCopy.CopyResult();
            WSCopy.CopyResult[] cResultArray = { cResult1, cResult2 };
            ArrayList fieldsInfo = new ArrayList();
    
            WSCopy.FieldInformation fFieledSubject = new WSCopy.FieldInformation();
            fFieledSubject.DisplayName = "Subject";
            fFieledSubject.Type = WSCopy.FieldType.Text;
            fFieledSubject.Value = "This is a subject";
            fieldsInfo.Add(fFieledSubject);
    
            fFieledSubject.DisplayName = "Document Reference";
            fFieledSubject.Type = WSCopy.FieldType.Text;
            fFieledSubject.Value = "0051054";
            fieldsInfo.Add(fFieledSubject);
    
            fFieledSubject.DisplayName = "Description";
            fFieledSubject.Type = WSCopy.FieldType.Text;
            fFieledSubject.Value = "description";
            fieldsInfo.Add(fFieledSubject);
    <br/>
    
    //this code is not working
            fFieledSubject.DisplayName = "Document Type";
            fFieledSubject.Type = WSCopy.FieldType.Lookup;
            fFieledSubject.Value = "1;#Accounting";
            fieldsInfo.Add(fFieledSubject); // -- end of code not working
    
            WSCopy.FieldInformation[] fFiledInfoArray = (WSCopy.FieldInformation[])fieldsInfo.ToArray(typeof(WSCopy.FieldInformation)); //{ fFieledSubject };
    
            FileStream strm = new FileStream(sourceUrl, FileMode.Open, FileAccess.Read);
            byte[] fileContents = new Byte[strm.Length];
            byte[] r = new Byte[strm.Length];
            int ia = strm.Read(fileContents, 0, Convert.ToInt32(strm.Length));
            strm.Close();
    
            //Copy the document from Local to SharePoint 
            uint copyresult = copyService.CopyIntoItems(sourceUrl, destinationUrl, fFiledInfoArray, fileContents, out cResultArray);
    


    Any help/suggestions are greatly appreciated.

    Thanks

    Damian

     

     

כל התגובות

  • יום שישי 19 אוגוסט 2011 15:05
     
     תשובה קוד כלול

    Lookup, File, and Computed field types are not supported with the copy web service. Nor is it supported with tthe SPListItem.CopyTo method. The reason for this, is there is no guarantee or check to see if the lookup list is located in the same site collection you are copying to. An alternative is to use RPC, client object model, http PUT along with the Lists web service to add the properties. There is also a problem with setting the ContentType with the copy web service.

    http://sharepointfieldnotes.blogspot.com/2009/09/uploading-content-into-sharepoint-let.html

     

    Below is some code to upload a file and set the metadata with the client object model.

     public static void UploadFile(string siteUrl, string listName, string contentTypeName, string targetFolder, string fileName)
    {
          ClientContext context = new ClientContext(siteUrl);
          Web web = context.Web;
          
          Folder docSetFolder = web.GetFolderByServerRelativeUrl(listName + "/" + targetFolder);
          context.ExecuteQuery();
    
          string documentUrl = "/" +listName + "/" + targetFolder + "/" + fileName;
    
          List list = context.Web.Lists.GetByTitle(listName);
    
          ContentTypeCollection listContentTypes = list.ContentTypes;
          context.Load(listContentTypes, types => types.Include
                           (type => type.Id, type => type.Name,
                           type => type.Parent));
          
    
          var result = context.LoadQuery(listContentTypes.Where
            (c => c.Name == contentTypeName));
    
          try
          {
            File existingFile = web.GetFileByServerRelativeUrl(documentUrl);
            context.Load(existingFile);
            context.ExecuteQuery();
            existingFile.DeleteObject();
            context.ExecuteQuery();
    
          }
          catch (Exception ex)
          {
          
          }
        
          ContentType targetDocumentSetContentType = result.FirstOrDefault();
    
          string contentTypeId = targetDocumentSetContentType.Id.ToString();
    
          FileCreationInformation fci = new FileCreationInformation();
          fci.Url = documentUrl;
          fci.Content = new byte[] { }; //byte[] take your stream and convert to byte array
           
    
          //get the folder's file collection
          FileCollection documentFiles = docSetFolder.Files;
          context.Load(documentFiles);
          context.ExecuteQuery();
    
          File newFile = documentFiles.Add(fci);
          context.Load(newFile);
          ListItem item = newFile.ListItemAllFields;
          context.Load(item);
    
          //start setting metadata here
          
          item["ContentTypeId"] = contentTypeId;
          item.Update();
    
          
          context.ExecuteQuery();
        
        
    }
    

     

     

     


    Blog | SharePoint Field Notes Dev Tool | ClassMaster
    • הוצע כתשובה על-ידי Akhilesh Nirapure יום רביעי 24 אוגוסט 2011 09:21
    • סומן כתשובה על-ידי Shimin Huang יום שישי 26 אוגוסט 2011 08:06
    •  
  • יום רביעי 24 אוגוסט 2011 09:18
     
     

    Thank you for your reply but the code is not working :/ Can you explain if there is need to any references expect for Microsoft.Sharepoint.Client?

    Thanks

  • יום חמישי 29 מרץ 2012 06:47
     
     

    I want to Add (Field values like Title ,Comment etc.) to a document library using Moss webservice. 

    Can i know how to do this.

    Thanks in advance.

  • יום שלישי 01 מאי 2012 13:11
     
     

    Title Question: Programmatically Add specific user to specific  Subsite in the sitecollection 

    Hi All,

    i am having one site collection created in the web application,

    i have to do the below requirement using custom webservice,

    now programmatically i want to add specific user to specific subsite in the site collection with specific permission,

    request to share your practical experience  on this.

    thanks in advance,