5

I need to upload files with specific information(custom fields) into sharepoint document library via custom windows application.

I have no idea how to do it, anyone has any examples how to do it either via web service or other methods?

[update]

Problem #2: During design time, i can add MS.sharepoint.dll without a problem, however when during compilation, it shows:

The type or namespace name 'SPWeb' could not be found (are you missing a using directive or an assembly reference?)

I'm pretty sure I added the dll and there's no error, only after compile it shows error.

2
  • There are various solutions to do so 1) Server Object Model 2) Client Object Model are you going to run this Windows Application in a Client Machine where SharePoint is not installed?
    – suryakiran
    Commented Sep 10, 2012 at 7:25
  • Lets assumed it's not installed, which method do i use ? or how do i install it ?
    – Melvin
    Commented Sep 10, 2012 at 7:59

5 Answers 5

1

Check this link, http://www.ktskumar.com/blog/2009/03/upload-document-from-local-machine-to-sharepoint-library/

This blog post explains how to upload the document to SharePoint using WebService

3
  • how do i know i have this or not ? copyService.Url = webUrl+”/_vti_bin/copy.asmx”;
    – Melvin
    Commented Sep 10, 2012 at 9:45
  • To check the URL, you have to browse through, http://<SiteUrl>/_vti_bin/copy.asmx . copyservice refers the WebService Reference. webUrl refers the website url Commented Sep 11, 2012 at 5:53
  • a) Link to your own blog but say it's your own blog. b) Please post the gist of your post and not just the link, some day this link might be broken.
    – James Love
    Commented Dec 18, 2013 at 8:33
5

You can use an HTTP PUT request to put document in the library. The code may be (adapt regarding your needs) :

Uri destUri = new Uri("http://server/sites/mysite/myweb/mylibrary/document.doc");
using(FileStream inStream = File.OpenRead(@"c:\doc\document.doc"))
{
    WebRequest req = WebRequest.Create(destUri);
    req.Method = "PUT";
    req.Credentials = CredentialCache.DefaultCredentials; // assuming windows Auth
    using(Stream outStream = req.GetRequestStream())
    {
        CopyStream(inStream, outStream);
    }
}

The main advantage of this method is that you don't have any sharepoint code. This is standard http webdav behavior.

The major drawback is that you don't have set any metadata.

To fix the later issue, you can use the standard web services of SharePoint.

5
  • in that case, i will have to use standard web services of SP .. do i need to install anything on client ?
    – Melvin
    Commented Sep 10, 2012 at 9:46
  • No. Simply add a service reference to the SharePoint web services in your VS project (lists.asmx or ListData.svc)
    – Steve B
    Commented Sep 10, 2012 at 9:48
  • how do i get or how do i know they are activated? the address to upload is sharepoint/sites/company/ProcessControl/Packing%20%20Shipping/… internal server btw.
    – Melvin
    Commented Sep 10, 2012 at 10:00
  • nvm, i got it.. just paste this instead of the aspx.. _vti_bin/copy.asmx
    – Melvin
    Commented Sep 10, 2012 at 10:07
  • Eventhough there is no way of setting the metadata, I like this solution best because it also enables uploading files to Documentsets and SharePoint Folders. Commented Jan 22, 2015 at 8:19
3

you can also use a Client Object Model Solution as Below

using (ClientContext clientContext = new ClientContext("http://contoso.com/subsite"))
{
    try
    {
        List documentLibrary = clientContext.Web.Lists.GetByTitle("List Title");
        FileCreationInformation newFile = new FileCreationInformation();
        newFile.Content = "Add File Stream Array";
        newFile.Overwrite = true;
        newFile.Url = "http://contoso.com/subsite/listurl/" + strFileName.Trim();
        Microsoft.SharePoint.Client.File uploadFile = documentLibrary.RootFolder.Files.Add(newFile);
    uploadFile.ListItemAllFields["Column Internal Name"] = "Column Internal Value";

        uploadFile.ListItemAllFields.Update();
        clientContext.ExecuteQuery();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

And regarding your Problem No 2: Please make sure that you have opened up the Visual Studio 2010 as Administrator. Also change your project settings such that it is compiled with .Net Framework 3.5 not with Framework 4.0

1

I would use Object model as shown in the following code sample:

String fileToUpload = @"C:\YourFile.txt";
String sharePointSite = "http://yoursite.com/sites/Research/";
String documentLibraryName = "Shared Documents";

using (SPSite oSite = new SPSite(sharePointSite))
{
    using (SPWeb oWeb = oSite.OpenWeb())
    {
        if (!System.IO.File.Exists(fileToUpload))
            throw new FileNotFoundException("File not found.", fileToUpload);                    

        SPFolder myLibrary = oWeb.Folders[documentLibraryName];

        // Prepare to upload
        Boolean replaceExistingFiles = true;
        String fileName = System.IO.Path.GetFileName(fileToUpload);
        FileStream fileStream = File.OpenRead(fileToUpload);

        // Upload document
        SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);

        // Commit 
        myLibrary.Update();
    }
}
3
  • problem no 2: during design time, i can add MS.sharepoint.dll without a problem, however when during compilation, it shows, The type or namespace name 'SPWeb' could not be found (are you missing a using directive or an assembly reference?) .. im pretty sure I added the dll and there's no error, only after compile it shows error.
    – Melvin
    Commented Sep 10, 2012 at 7:58
  • Can you ask another question for that or you should had already mentioned it in your question or edit your existing question Commented Sep 10, 2012 at 8:00
  • i have edited my question
    – Melvin
    Commented Sep 10, 2012 at 8:06
-2

http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/programmatically-adding-document-to-sharepoint-document-library/

2
  • i do not see the windows sharpoint services under references, im using vs2010
    – Melvin
    Commented Sep 10, 2012 at 7:56
  • While this may theoretically answer the question, we prefer inclusion of the essential parts of the answer here, and to provide the link for reference. See answer for general guidelines.
    – SPDoctor
    Commented Sep 10, 2012 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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