2

We have a SharePoint server with admin account in it and I was connecting it from my local machine through IP and SharePoint port manually.

But I need to write a program which needs to upload the files into the SharePoint server from the local machine to server. Is it possible in using WinForms ? or only possible in web services?

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();
    }
}

using the above code the file is getting inserted only on the machine where SharePoint installed.

tried using the above code and I am getting error from the following line:

using (SPSite oSite = new SPSite(sharePointSite))

and the error was

"The Web application at http://server:port/ could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application"

and am not able to upload the file. But if I copied and paste the same URL in my local machine I can able to access the SharePoint deployed in server and I can even upload files manually from my local machine.

How to upload a file in SharePoint server from the local machine connected with LAN? or Only web service is possible for uploading from local machine to server ?

0

Yes, you can upload your file. Basically, for this kind of approach, .Net Managed Client Side Object Model (CSOM) is used.

For that you need to manually add the Microsoft.SharePoint.Client.dll & Microsoft.SharePoint.Client.Runtime.dll reference in your project, so that you can use the ClientContext object of SharePoint which will allow you to upload the file after successful authentication.

Here are examples for complete basic list of operation that you can see for reference.

As per your question, you can find the good article here : How to upload/download a document in SharePoint 2010 using Client Context Object Model

As you have not mentioned in the question the version of SharePoint, I have only shared links of SharePoint 2010 only. You can find those link for SharePoint 2013 as well.

The code above you have mentioned is the server side object model of SharePoint and it does only work where SharePoint is installed.

  • Bro i tried it and it does't work :( :( @Pradip – Dah Sra Aug 25 '15 at 10:11
0

siteURL = Main URl of the sharepoint (eg) "http://10.0.0.14:48487/";

documentListName = any of the folders in shrepoint (eg) Shared Documents

documentName = name of the file (eg)sampleword.docx , readme.txt etc

documentStream = the byte format of the file which we going to upload.

(eg)byte[] bytefile = System.IO.File.ReadAllBytes(filepath+filename);

public static void UploadDocument(string siteURL, string documentListName, string documentListURL,string documentName, byte[] documentStream = null)
    {  

     try
        {
        using (SP.ClientContext clientContext = new SP.ClientContext(siteURL))
        {

            #region"Only if you have credentials"
            NetworkCredential Cred = new NetworkCredential("username", "password");
            clientContext.Credentials = Cred;
            #endregion


            SP.List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);

            var fileCreationInformation = new SP.FileCreationInformation();
            //Assign to content byte[] i.e. documentStream

            fileCreationInformation.Content = documentStream;
            //Allow owerwrite of document

            fileCreationInformation.Overwrite = true;
            //Upload URL

            fileCreationInformation.Url = documentName;

            Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(
                fileCreationInformation);


            uploadFile.ListItemAllFields.Update();
            clientContext.ExecuteQuery();

        }
    }
    catch (Exception ex)
    {
    }
}

}

This works perfect for me :) :)

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.