Showing posts with label upload files to document library. Show all posts
Showing posts with label upload files to document library. Show all posts

Thursday, November 17, 2011

Upload file to document library and update the meta data using client context

public ListItem UploadDocument(String fileName, String filePath, Dictionary<string, object> metaDataList)
{
SP.ClientContext ctx = new SP.ClientContext(“http://yoursharepointURL”);

Web web = ctx.Web;

FileCreationInformation newFile = new FileCreationInformation();

newFile.Content = System.IO.File.ReadAllBytes(@"C:\TestFile.doc");

newFile.Url = "/" + fileName;

List docs = web.Lists.GetByTitle(“Shared Documents”);

Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);

// Set the file to be uploaded and define the fields to be returned
context.Load(uploadFile,
i => i.ListItemAllFields.Id
i => i.ListItemAllFields.DisplayName,
i => i.ListItemAllFields["Title"],
i => i.ListItemAllFields["FileRef"],
i => i.ListItemAllFields["Created_x0020_By"],
i => i.ListItemAllFields["File_x0020_Size"]);

SPClient.ListItem item = uploadFile.ListItemAllFields;

//Set the metadata
foreach (var meta in metaDataList)
{
item[meta.Key] = meta.Value;
}

item.Update();

context.ExecuteQuery();

return item;
}

Wednesday, November 16, 2011

Upload a File to a SharePoint Document Library


using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;


namespace DevHoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            DocLibHelper docLibHelper = new DocLibHelper();
            Dictionary<string, object> properties = new Dictionary<string, object>();
            properties.Add("Title", "Test Title");
            //Create or overwrite text file test.txt in 'Docs' document library creating folder 'Test Folder' as required.
            docLibHelper.Upload("http://localhost/Docs/Test Folder/test.txt", System.Text.Encoding.ASCII.GetBytes("Test text."), properties);
        }
    }
 
    public class DocLibHelper
    {
        ListsService.Lists m_listService;
        ICredentials m_credentials;
        ListInfoCollection m_lists;
 
        public DocLibHelper()
        {
            m_credentials = CredentialCache.DefaultCredentials;
            m_listService = new ListsService.Lists();
            m_listService.Credentials = m_credentials;
            m_lists = new ListInfoCollection(m_listService);
        }

Upload document from Local Machine to SharePoint Library using WebService


Parameters,

SourceUrl:
A String that contains the absolute source URL of the document to be copied.
DestinationUrls:
An array of Strings that contain one or more absolute URLs specifying the destination location or locations of the copied document.
Fields:
An array of FieldInformation objects that define and optionally assign values to one or more fields associated with the copied document.
Stream:
An array of Bytes that contain the document to copy using base-64 encoding.
Results:
An array of CopyResult objects, passed as an out parameter.
Return Value:
A UInt32 that returns 0 to indicate that the operation has completed. (There is also an out parameter containing an array of CopyResult objects.)
Below i’m providing a code for uploading a document to SharePoint Library,

Sunday, November 13, 2011

Upload a File to a SharePoint Site from a Local Folder


Aspx Page

<form id="Form1" method="post" runat="server">
   <SharePoint:FormDigest runat="server" />
   <input id="File1" type="file" runat="server" title="upldFileBox">
   <asp:Button id="Button1" runat="server" 
      Text="Upload File"></asp:Button>
   <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</form>

Code Behind

Tuesday, November 8, 2011

SharePoint Upload File in Library


public void UploadFileInLibrary(string siteUrl, string webName, string libraryName, string subfolderPath, string fileName)
{
    using (ClientContext clientContext = new ClientContext(siteUrl))
    {

        string uploadLocation = Path.GetFileName(fileName);
        if (!string.IsNullOrEmpty(subfolderPath))
        {
            uploadLocation = string.Format("{0}/{1}", subfolderPath, uploadLocation);
        }
        uploadLocation = string.Format("/{0}/{1}/{2}", webName, libraryName, uploadLocation);
        var list = clientContext.Web.Lists.GetByTitle(libraryName);
        var fileCreationInformation = new FileCreationInformation();
        fileCreationInformation.Content = System.IO.File.ReadAllBytes(fileName);
        fileCreationInformation.Overwrite = true;
        fileCreationInformation.Url = uploadLocation;
        list.RootFolder.Files.Add(fileCreationInformation);
        clientContext.ExecuteQuery();
    }
}

Featured Posts

Kali Linux Remote Desktop: Access GNOME from Windows Using Native RDP

  Kali Linux + GNOME 50 + GNOME Remote Desktop + Windows Remote Desktop (MSTSC) Getting a full GNOME desktop remotely on Kali Linux can be ...