7

I'm new to web services and i've been trying to create a custom one using this tutorial:

http://msdn.microsoft.com/en-us/library/ms464040.aspx

I've named my service as Files and created a new method on the webservice called UploadDocument:

[WebMethod]
    public string UploadDocument(string fileName, byte[] fileContents, string pathFolder)
    {
        if (fileContents == null)
        {
            return "Null Attachment";
        }

        try
        {
            int iStartIndex = pathFolder.LastIndexOf("/");
            string sitePath = pathFolder.Remove(iStartIndex);
            string folderName = pathFolder.Substring(iStartIndex + 1);

            SPSite site = new SPSite(sitePath);
            SPWeb web = site.OpenWeb();

            SPFolder folder = web.GetFolder(folderName);

            string fileURL = fileName;

            folder.Files.Add(fileURL, fileContents);

            if (folder.Files[fileURL].CheckedOutByUser.Name != "")
            {
                folder.Files[fileURL].CheckIn("File Checked In");
            }

            return "File added successfully!";

        }
        catch (System.Exception ex)
        {
            return "Error: " + ex.Source + " - " + ex.Message;
        }
    }

I've done exactly as it's listed there, all the same name, methods etc.. and when i get to the point where you're asked to create the application that's going to consume the web service, the tutorial gives the following lines:

ServiceFolder.Files myUploadService = new ServiceFolder.Files();
myUploadService .UseDefaultCredentials = true;
MessageBox.Show(myUploadService.HelloWorld());

but on my application, i can't call the web service as it's telling me to do, when i type

ServiceFolder.

it won't have the option to call the classes i've created. it only gives me some options that are not the ones listed on the tutorial:

ServiceFolder.FilesSoap (Interface)
ServiceFolder.FilesSoapChannel (Interface)
ServiceFolder.FilesSoapClient (Class)
ServiceFolder.UploadDocumentCompletedEventArgs (Class)
ServiceFolder.UploadDocumentRequest (Class)
ServiceFolder.UploadDocumentRequestBody (Class)
ServiceFolder.UploadDocumentResponse (Class)
ServiceFolder.UploadDocumentResponseBody (Class)

Am i missing something? doing something wrong? i've tried to recreate the service a couple times and always get the same thing.

Thanks in advance.

12

That walkthrough has some, er, issues...

I wrote a blog post with an alternate version that you may find a little easier to follow.

Walkthrough: Creating a Custom ASP.NET (ASMX) Web Service in SharePoint 2010

  • Rob.. i don't have words to express how grateful i am for this walkthrough. Just one little question. How do you invoke the GetSiteListCount() method in your site/application? I've added the service reference, but i can't find how to call that method and use it's result on my code. Thanks a lot – C. Hoffmann Nov 7 '11 at 13:39
  • I've updated the blog post to demonstrate how to create a client for the service. – Rob Windsor Nov 7 '11 at 14:42
  • Rob. It worked like a charm. Thank so you much for your help. – C. Hoffmann Nov 7 '11 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.