Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
    
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 Sep 10 '12 at 7:25
    
Lets assumed it's not installed, which method do i use ? or how do i install it ? –  Melvin Sep 10 '12 at 7:59
add comment

5 Answers 5

up vote 1 down vote accepted

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

share|improve this answer
    
how do i know i have this or not ? copyService.Url = webUrl+”/_vti_bin/copy.asmx”; –  Melvin Sep 10 '12 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 –  Shantha Kumar Thambidurai Sep 11 '12 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 Dec 18 '13 at 8:33
add comment

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.

share|improve this answer
    
in that case, i will have to use standard web services of SP .. do i need to install anything on client ? –  Melvin Sep 10 '12 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 Sep 10 '12 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 Sep 10 '12 at 10:00
    
nvm, i got it.. just paste this instead of the aspx.. _vti_bin/copy.asmx –  Melvin Sep 10 '12 at 10:07
add comment

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();
    }
}
share|improve this answer
    
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 Sep 10 '12 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 –  SharePointer Sep 10 '12 at 8:00
    
i have edited my question –  Melvin Sep 10 '12 at 8:06
add comment

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

share|improve this answer
add comment

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

share|improve this answer
    
i do not see the windows sharpoint services under references, im using vs2010 –  Melvin Sep 10 '12 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 Sep 10 '12 at 15:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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