RE: error on uploadDocument webmethod of "Writing Custom Web Services.

From: david (david_at_discussions.microsoft.com)
Date: 02/17/05


Date: Thu, 17 Feb 2005 01:49:04 -0800

Fixed.

it's a great article but with an terrible bug on the code example (terrible
because i've spent three hours debugging my own code to discover this
problem); on the spfileswsdl.aspx, the xml for uploadDocument webmethod is:

      <s:element name="UploadDocument">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="strFilename"
type="s:string" />
            <s:element minOccurs="0" maxOccurs="1" name="fileContents"
type="s:base64Binary" />
            <s:element minOccurs="0" maxOccurs="1" name="pathFolder"
type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>

notice the property name of the first and the third elements; strFilename
and pathFolder.

and the webmethod method header is:

[WebMethod]
public string UploadDocument(string fileName, byte[] fileContents, string
targetFolder)

notice that here, the first parameter is fileName (instead of strFilename)
and targetFolder (instead of pathFolder)

so, as the name of the method params are not equal, the first and the third
params are received as null params, and the second is received Ok. If you
rename the webmethod params as the spfileswsdl.aspx params and refresh the
webmethod, the params are received ok

"david" wrote:

> Hello!
> i'm trying to make a sharepoint web service to upload documents, and i've
> seen the excellent "Writing Custom Web Services for SharePoint Products and
> Technologies"
> (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_sp2003_ta/html/odc_writingcustomwebservicesforsppt.asp)
> But i have a really strange problem with the example attached
> (ODC_WritingCustomWebServicesSampleSPPT.EXE)
> I've downloaded and installed it. I've added it to a winform application and
> then , when i developed a code snippet to upload a document and i'e tested
> it, i've obtained this error:
>
> "the parameter cannot be null" (or something like this)
> The fact is that my client method is:
>
> public static string uploadDocument(string strPath,string strDestination)
> {
> uploadService.SPFiles svcDocLib = null;
> string result=null;
> try
> {
> svcDocLib = new uploadService.SPFiles();
> svcDocLib.Credentials = AuthGetAutheticationData();
> svcDocLib.Url = SharePointHost + UploadServiceName;
> string strFile = strPath.Substring(strPath.LastIndexOf("\\") + 1);
>
> FileStream fStream = new FileStream(strPath, System.IO.FileMode.Open);
> byte[] binFile = new byte[(int)fStream.Length];
> fStream.Read(binFile, 0, (int)fStream.Length);
> fStream.Close();
> result = svcDocLib.UploadDocument(strFile, binFile, strDestination);
> }
> catch (Exception e)
> {
> OnError("upload methods","uploadDocument Error",e.Message.ToString());
> result ="NEXTEL ERROR:"+e.Message.ToString();
> }
> finally
> {
> if (svcDocLib!=null)
> svcDocLib.Dispose();
> }
> return (result);
> }
>
> i've tested that the parameters that receive svcDocLib.UploadDocument are
> not null, but, when the webmethod on the server is invoked,the values, for
> some reason of the first and the third params are null (strFile is received
> as null, binFile CORRECT, strDestination is received as null)
>
> this is the upload document webmethod. As you can see, i've debugging with a
> string variable and the value returned says that filename is null,
> targetfolder is null, and the size of filecontents is the length of the
> uploaded file in bytes, so this param is received ok.
>
>
> [WebMethod]
> public string UploadDocument(string fileName, byte[] fileContents, string
> targetFolder)
> {
> string result ="Init"+'\n';
> if (fileName=="")
> result+= "filename Empty"+'\n';
> if (fileName==null)
> result+="filename Null"+'\n';
>
> if (targetFolder == "")
> result+="targetFolder empty"+'\n';
> if (targetFolder == null)
> result+="targetFolder null"+'\n';
> if (fileContents==null)
> result+="fileContents null"+'\n';
> else
> result+="fileContents"+fileContents.Length.ToString()+'\n';
>
> result+="params:"+'\n'+"fileName:"+fileName+'\n'+"fileContents.Size="+(fileContents!=null).ToString()+"targetFolder:"+targetFolder+'\n';
> if (fileContents == null)
> {
> result+= "Null Attachment"+'\n';
> return result;
> }
> try
> {
> SPFolder folder = targetWebSite.GetFolder(targetFolder);
> result+="folder!=null -->"+(folder!=null).ToString()+'\n';
> SPFile newFile = folder.Files.Add(fileName, fileContents);
> result+="newFile!=null -->"+(newFile!=null).ToString()+'\n';
> return result+"\n"+newFile.Title + " created " +
> newFile.TimeCreated.ToLongDateString();
> }
> catch (System.Exception ee)
> {
> result+= ee.Message + ee.Source + "::targetfolder="+targetFolder+'\n';
> return result;
> }
> }
>
>
> any ideas?
> i'm really confused, this is really strange, isn't it?
>
> Thanks
>



Relevant Pages