Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Can we upload document to Sharepoint using a C# code without using Microsoft.Sharepoint DLL?
Posted
Comments
Sunasara Imdadhusen 4-Jun-13 6:14am    
Why u don't want to use Sharepoint dll?
suhanaarora 4-Jun-13 7:29am    
Because I heard dll works only if Sharepoint is installed and I do not want this dependency

You can upload documents to SharePoint libraries using the Object Model or SharePoint Webservices.

Upload using Object Model:

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 this answer
 
Comments
Member 10397912 13-Jan-14 4:36am    
I want to upload a file into an document library having flag value. When we do upload a file flag value should be "Zero" and when we click save flag value should be updated as "one"
Ankur.15 26-Sep-16 2:20am    
Hi,
I tried this code and also added Microsoft.Sharepoint Namespace.
But still it is showing red line error in SPSite and SPWeb.
Can you please help me out how to solve it?
Thanks
// First locate your sharepoint site and hit service in browser.
// It is like "http://fullsitename/_vti_bin/lists.asmx"
// If you do not get 404 then it accessible and seems working for current user.
// Add its reference to project.

// SharePoint 2010 provides web service list.asmx which can be used.
// It can be used with Jquery Ajax calls as well.
// In below example i updated listitem by attaching attachment to it.
// Care fully add web reference in project or you will not get ServiceWebReference.Lists class.
// ServiceWebReference can have different name, as you prefer.
// This is working POC...... Hope this help :)

private void button1_Click(object sender, EventArgs e)
{
string srcUrl = @"C:\Users\prasads\Downloads\birthdaypic.gif";

if (!File.Exists(srcUrl))
{
throw new ArgumentException(String.Format("{0} does not exist",
srcUrl), "srcUrl");
}

FileStream fStream = File.OpenRead(srcUrl);
string fileName = fStream.Name.Substring(3);
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();

ServiceWebReference.Lists listService = new ServiceWebReference.Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;

try
{
string addAttach = listService.AddAttachment("mylistname", "1", fileName, contents);

}

catch (System.Web.Services.Protocols.SoapException ex)
{
// catch error
}
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900