none
Upload document into document library RRS feed

  • Question

  • Hi Guys,

    What is the best way to upload document + updating associated methadata

    I have seen http://msdn.microsoft.com/en-us/library/dd587349(office.11).aspx

    But it is not updating methadata?

    Only one way I have seen is to use

    Microsoft.SharePoint.Client

    But in this case I need to configure access account

    Is it the best way to do it?

            bool SaveDocument(string TitleID, string DocumentType, string Name, FileUpload FileUpload)
            {
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate() {
    
                    using (var context = new ClientContext(SPContext.Current.Web.Site.Url))
                    {
                        Site site = context.Site;
                        Web web = site.RootWeb;
    
                        List list = web.Lists.GetByTitle("Documents");
    
                        FileCreationInformation fileParameters = new FileCreationInformation();
                        fileParameters.Url = Name;
                        fileParameters.Overwrite = true;
    
                        //Getting the file content 
                        Stream fStream = FileUpload.PostedFile.InputStream;
                        byte[] contents = new byte[fStream.Length];
                        fStream.Read(contents, 0, (int)fStream.Length);
                        fStream.Close();
    
                        fileParameters.Content = contents;
    
    
                        Microsoft.SharePoint.Client.File file = list.RootFolder.Files.Add(fileParameters);
    
                        Microsoft.SharePoint.Client.ListItem listItem = file.ListItemAllFields;
    
                        listItem["Title"] = Name;
                        listItem["ApplicationID"] = TitleID;
                        listItem["DocumentID"] = "765796i908-878076";
                        listItem["Type"] = DocumentType;
                        listItem["Size"] = contents.Length;
                        listItem.Update();
                        context.ExecuteQuery();
                    }
                        
                    });
                 }
                 catch (Exception ex)
                 {
                            //TODO
                            return false;
                 }
    
              
    
    
                
                return true;
            }

    Thursday, November 24, 2011 12:55 AM

Answers

  • Have you tried this way?

    public static void UploadFile(string siteUrl, string targetFolder, string fileName)
     {
          ClientContext context = new ClientContext(siteUrl);
          Web web = context.Web;
          Folder docSetFolder = web.GetFolderByServerRelativeUrl(targetFolder);
          context.ExecuteQuery();
    
          string documentUrl = "/" + targetFolder + "/" + fileName;
    
          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
          string contentTypeId = string.Empty;
          item["ContentTypeId"] = contentTypeId;
          item.Update();
    
          context.ExecuteQuery();
        
        
    }
    

    Reference : http://social.technet.microsoft.com/Forums/en/sharepoint2010programming/thread/1cbbba15-3607-42e5-800a-e46ee0bfa6f0


    Bhushan | http://www.passionatetechie.blogspot.com | http://twitter.com/BhushanGawale
    Thursday, November 24, 2011 3:39 AM

All replies

  • Have you tried this way?

    public static void UploadFile(string siteUrl, string targetFolder, string fileName)
     {
          ClientContext context = new ClientContext(siteUrl);
          Web web = context.Web;
          Folder docSetFolder = web.GetFolderByServerRelativeUrl(targetFolder);
          context.ExecuteQuery();
    
          string documentUrl = "/" + targetFolder + "/" + fileName;
    
          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
          string contentTypeId = string.Empty;
          item["ContentTypeId"] = contentTypeId;
          item.Update();
    
          context.ExecuteQuery();
        
        
    }
    

    Reference : http://social.technet.microsoft.com/Forums/en/sharepoint2010programming/thread/1cbbba15-3607-42e5-800a-e46ee0bfa6f0


    Bhushan | http://www.passionatetechie.blogspot.com | http://twitter.com/BhushanGawale
    Thursday, November 24, 2011 3:39 AM
  • It is pretty much the same way, my concern is ClientContext,

    in simple case it is fine, but when it needs more security you need to montain credentials,

    I was hopping to evoid it by using "normal" sharepoint objects model.

    May be I am wrong and the way thry Client API is the way to go..

    Friday, November 25, 2011 5:15 AM