none
How to upload a file with silverlight client object model?

    Question

  • I have been trying to upload files to a sharepoint doclib with a silverlight application. It works fine, as long as the file is not larger than about 50kb. if i try to upload larger files, i get an error:

    System.Net.WebException The remote server returned an error: NotFound

     

    See my code below 

    ClientContext clientCxt = new ClientContext("http://dev");
    
      List docs;
    
      Site mysite;
    
      Folder myfolder;
    
    docs = clientCxt.Web.Lists.GetByTitle("Shared Documents");
    
       mysite = clientCxt.Site;
    
       myfolder = clientCxt.Web.Lists.GetByTitle("Shared Documents").RootFolder;
    
       clientCxt.Load(mysite);
    
       clientCxt.Load(docs);
    
       clientCxt.Load(myfolder);
    
       clientCxt.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnSuccess), new ClientRequestFailedEventHandler(OnFailed));
    
    .
    
    .
    
    .
    
    .
    
    .
    
    
    
    foreach (FileInfo FileDropped in FilesDropped)
    
        {
    
         FileStream fs = FileDropped.OpenRead();
    
         
    
          byte[] FileContent = new byte[fs.Length];
    
          FileCreationInformation File = new FileCreationInformation();
    
          int dummy = fs.Read(FileContent, 0, (int)fs.Length);
    
          File.Content = FileContent;
    
          File.Url = mysite.Url + myfolder.ServerRelativeUrl + "/" + FileDropped.Name;
    
          File.Overwrite = true;
    
          Files.Add(File);
    
          OneFileUploaded = true;
    
    
    
    
    
          if (OneFileUploaded)
    
           clientCxt.ExecuteQueryAsync(OnSuccess, OnFailed);
    
        }   
    
    
    Saturday, April 17, 2010 9:03 PM

Answers

  • could you try by setting the SPClientRequestServiceSettings.MaxReceivedMessageSize to a higher value from a server side console application ?

    I know this setting works with the Managed client OM, but I'm not sure it has any impact on the Silverlight OM.

     


    Serge Luca; blog: http://sergeluca.spaces.live.com Devoteam Belgium
    Sunday, April 18, 2010 9:22 AM

All replies

  •  

    2 possibilities :

    1. set the following value of SPClientRequestServiceSettings.MaxReceivedMessageSize
    2. Http DAV : Microsoft.SharePoint.Client.File.SaveBinaryDirect

    You will find an example here :

    http://blogs.msdn.com/sridhara/archive/2010/03/12/uploading-files-using-client-object-model-in-sharepoint-2010.aspx

    Hope this helps

    Serge Luca


    Serge Luca; blog: http://sergeluca.spaces.live.com Devoteam Belgium
    • Proposed as answer by sven anderson Sunday, April 18, 2010 1:35 PM
    Sunday, April 18, 2010 7:24 AM
  • Thanks for your reply.

    But in silverlight client object model there is no Microsoft.SharePoint.Client.File.SaveBinaryDirect method.

    And the SPClientRequestServiceSettings is in the Microsoft.SharePoint.Administration Namespace and i can't use that one in a silverlight application because it was not built against silverlight.

    Sunday, April 18, 2010 8:39 AM
  • you are right I didn't notice the word "silverlight" in your post :-)

    Try this sample http://technicaldishes.blogspot.com/2010/01/silverlight-clientobject-model.html and let me know if that works

     


    Serge Luca; blog: http://sergeluca.spaces.live.com Devoteam Belgium
    Sunday, April 18, 2010 8:42 AM
  • That's basically what i am doing, if you look at my code above. And it does work fine, as long as the files don't exeed 50kb. But i need way more then just 50kb......
    Sunday, April 18, 2010 9:03 AM
  • could you try by setting the SPClientRequestServiceSettings.MaxReceivedMessageSize to a higher value from a server side console application ?

    I know this setting works with the Managed client OM, but I'm not sure it has any impact on the Silverlight OM.

     


    Serge Luca; blog: http://sergeluca.spaces.live.com Devoteam Belgium
    Sunday, April 18, 2010 9:22 AM
  • Hey very cool, that worked. And my silverlight application is working too now. Well that is not the best solution yet, but at least a workaround for now.

    Do you know whats the impact of setting this to a higher value? Will sharepoint run into any problems now?

    Sunday, April 18, 2010 9:30 AM
  • I'm pretty sure  it doesn't have any bad impact on Sharepoint; it is probably a way to prevent people from overloading the server.
    Serge Luca; blog: http://sergeluca.spaces.live.com Devoteam Belgium
    Sunday, April 18, 2010 10:08 AM
  • I'm pretty sure  it doesn't have any bad impact on Sharepoint; it is probably a way to prevent people from overloading the server.
    Serge Luca; blog: http://sergeluca.spaces.live.com Devoteam Belgium
    • Proposed as answer by sven anderson Sunday, April 18, 2010 1:34 PM
    Sunday, April 18, 2010 10:08 AM