Search code examples
c#sharepointspsite

How to upload a file in share point server from a local machine connected through lan using c#


I am really new to this share point stuffs.We have a share point server with admin account in it and i was connecting it from my local machine through ip and share point port manually. Nut i need to write a program which needs to upload the files into the share point 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();
}

}

tried using the above code and i 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..??


Solution

  • 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 :) :)