Click here to Skip to main content
13,835,256 members
Rate this:
 
Please Sign up or sign in to 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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();
    }
}
   
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

// 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
}
}
   
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190114.1 | Last Updated 4 Jun 2013
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100