none
Uploading .pdf file to sharepoint using sharepoint web services

    Question

  • Hi,

    I need to write a web service to upload documents to sharepoint.  Now my web service should not use the sharepoint API directly, instead I need to use the exposed web services for lists etc.  So, I can not use SPSite, SpWeb or Context directly. 
    I wrote the below code to upload the file and it seems to work.  But, it does not update meta data and it does not work for .pdf file.  The .pdf file gets uploaded fine but when I try to open it from sharepoint, it gives me the error - 'File does not begin with '%PDF-'

    Please find my code below -

     byte[] filecontents = File.ReadAllBytes(localFile);


                        string sSPURL = ConfigurationManager.AppSettings["SharePointServer"].ToString();
                        string sDocLib = ConfigurationManager.AppSettings["DocLibrary"].ToString();
                        sDocLib = sDocLib.Replace(" ", "%20");
                        string sUser = ConfigurationManager.AppSettings["User"].ToString();
                        string sPwd = ConfigurationManager.AppSettings["Pwd"].ToString();
                        string sRemoteFileUrl;

                        System.Net.NetworkCredential nc = new System.Net.NetworkCredential(sUser, sPwd);
                        //e.g. :  http://stratocumulus.sdi.na.xde3.xerox.org:39007/sites/EIP/Shared%20Documents/Forms/AllItems.aspx
                        sRemoteFileUrl = sSPURL + "/" + sDocLib + "/" + remoteFile.Trim();

                        sRemoteFileUrl = sRemoteFileUrl.Replace(" ", "%20");
                        sRemoteFileUrl = sRemoteFileUrl.Replace("\\", "/");

                        WebClient wc = new WebClient();
                        wc.Credentials = nc;
                        try
                        {
                            r = wc.UploadData(sRemoteFileUrl, "PUT", filecontents);
                        }
                        catch
                        { }

    This works for all except .pdf file. Can anyone tell me what am I missing. 
                       
    Tuesday, February 24, 2009 6:47 PM

Answers

  • Figured out the problem.  The version of acrobat reader installed on the server was a very old one.  It was not able to automatically convert the file uploaded and was showing the error.  Why was it showing that particular error - can't figure out.  But, updating acrobat reader on the server solved the problem.  
    • Marked as answer by swatijain Wednesday, February 25, 2009 1:58 PM
    Wednesday, February 25, 2009 1:58 PM

All replies

  • Seems filecontents is incorrect.
    Try to find how pdf is loaded.
    Wednesday, February 25, 2009 2:00 AM
  • Figured out the problem.  The version of acrobat reader installed on the server was a very old one.  It was not able to automatically convert the file uploaded and was showing the error.  Why was it showing that particular error - can't figure out.  But, updating acrobat reader on the server solved the problem.  
    • Marked as answer by swatijain Wednesday, February 25, 2009 1:58 PM
    Wednesday, February 25, 2009 1:58 PM