PostRank Topblogs 2009 - #20 in Sharepoint

Windows Live Alerts web tracker
Chat with me if I'm online!
search blog
most popular
MCP MCTS MCT MVP

SP 2010: Uploading files using the Client OM in SharePoint 2010

Author: Tobias Zimmergren
http://www.zimmergren.net | http://www.tozit.com | @zimmergren

Introduction

In this article I will guide your through the process of uploading a document to any chosen document library in SharePoint 2010 through the Client Object Model.

This application has a very simple usage scenario:

  1. Type in the URL to your site where you want to upload your file
  2. Choose one of the available Document Libraries
  3. Click Upload a Document and you'll get a browse-dialog to choose the file

Example:

1) Enter a servername and click Fetch Libraries
2) Select the Document library you want to upload your file to
image

3) Browse for a local file on your filesystem
image

4) Click the magic button (Upload) and you'll see your document shoot straight into SharePoint from your client machine(s)
image

How to utilize the Client Object Model in SharePoint 2010 to upload files

The most important thing to learn about when it comes to uploading files with the Client OM is to master the FileCreationInformation class that comes with the Client OM.

Take a look at this complete snippet to see how you can upload a file:

private void btnUploadDocument_Click(object sender, EventArgs e)
{
    string library = listBox1.SelectedItem.ToString();
 
    OpenFileDialog openDialog = new OpenFileDialog();
    openDialog.Multiselect = false;
 
    if(openDialog.ShowDialog() == DialogResult.OK)
    {
        SP.ClientContext ctx = new SP.ClientContext(tbSite.Text);
 
        var web = ctx.Web;
 
        var fciNewFileFromComputer = new SP.FileCreationInformation();
        fciNewFileFromComputer.Content = File.ReadAllBytes(openDialog.FileName);
        fciNewFileFromComputer.Url = Path.GetFileName(openDialog.FileName);
 
        SP.List docs = web.Lists.GetByTitle(library);
        SP.File uploadedFile = docs.RootFolder.Files.Add(fciNewFileFromComputer);
 
        ctx.Load(uploadedFile);
        ctx.ExecuteQuery();
 
        // Tell your user that the file is uploaded. Awesomeness has been done!
        MessageBox.Show(openDialog.FileName + " uploaded to " + library);
    }
}

Things to note here is that I'm currently not changing the authentication for this application. That means it'll be using your Windows/Domain Credentials.

Learn more about the authentication options here:
http://www.zimmergren.net/archive/2009/11/30/sp-2010-getting-started-with-the-client-object-model-in-sharepoint-2010.aspx

Summary, References & Download

With this sample application you can easily upload any file to any of your SharePoint 2010 document libraries by first entering the URL to the site, then selecting your library and finally browsing for a file and click OK.

Easily enough, you can change or extend this project the way you want it to work if you've got specific requirements to upload files using the Client OM.

References

  1. Getting Started with the Client Object Model
  2. FileCreationInformation

Download project

You can download this sample project here.

Enjoy this piece of awesomeness :-)

 


Published: Jun-10-10 | 21 Comments | 0 Links to this post

SP 2010: Programmatically work with External Lists (BCS) using the Client Object Model

Author: Tobias Zimmergren
http://www.zimmergren.net | http://www.tozit.com | @zimmergren

Introduction

Article 3 in the small BCS-series:

1. SP 2010: Getting started with the Business Connectivity Services (BCS)
2. SP 2010: Programmatically work with External Lists (BCS) in SharePoint 2010
3. SP 2010: Programmatically work with External Lists (BCS) using the Client Object Model

In my previous article in the series, I talked about how easy it is to fetch information using the standard SharePoint server API-approach. In this article I will talk about how you can access data in your BCS data source, by utilizing the Client Object Model.

Client Object Model code to read from the external list

Just like I described in my previous article, you now have the awesome ability to work with data in your External Lists (connected to a data source using BCS) - namely, you can use the standard SharePoint APIs.

Since I've shown you how you can do this using the Server Object Model, I thought I could take another spin at it and show you the code for doing basically the same with the Client Object Model.

The underlying data

As with my previous article, I'm still using the same data source as I set up in my first article - the "ProductList" table in my SQL Server database called "Zimmergren_DB"

As seen in the SQL Server Management Studio:
image

Let's fetch the data using a Windows Forms application that utilizes the Client Object Model!

I've designed a Windows Forms application to utilize the .NET Client Object model (in contrary to using the Silverlight client object model or JavaScript client object model).

It looks like this:
 image

When you click the fancy button called "Get External Data", it will use the Client Object Model to fetch the records from the external list (from the SQL server) and display them in a DataGridView. Nothing fancy.

The code!

With no further delays or chit-chat, here's the simple code!

    // Define the Client Context (as defined in your textbox)
    SP.ClientContext context = new SP.ClientContext(tbSite.Text);
    SP.Web site = context.Web; 

    var ProductList = site.Lists.GetByTitle(tbList.Text); 

    SP.CamlQuery camlQueryAwesomeness = new SP.CamlQuery();  

    IQueryable<SP.ListItem> productItems =
                                      ProductList.GetItems(camlQueryAwesomeness); 

    IEnumerable<SP.ListItem> externalList =
                                      context.LoadQuery(productItems);

    // This is where we actually execute the request against the server!
    context.ExecuteQuery(); 

    // Retrieve the products from the product list using some fancy LINQ
    var productListData = from product in externalList
      select new
      {
          // We're never pointing to the field at the 0-index
          // because it's used by the BDC Identity itself. Hence our elements start at 1.
          ProductID = product.FieldValues.ElementAt(1).Value.ToString(),
          ProductName = product.FieldValues.ElementAt(2).Value.ToString(),
          ProductDescription = product.FieldValues.ElementAt(3).Value.ToString()
      }; 

    // Simply clear the rows and columns of the GridView
    gvProducts.Rows.Clear();
    gvProducts.Columns.Clear(); 

    // Add the columns we need (ProductID, Name, Description)
    gvProducts.Columns.Add("ProductID", "ProductID");
    gvProducts.Columns.Add("Name", "Product Name");
    gvProducts.Columns.Add("Description", "Product Description");
    foreach (var product in productListData)
    {
        // For each product in the list, add a new row to the GridView
        gvProducts.Rows.Add(
                                 product.ProductID,
                                 product.ProductName,
                                 product.ProductDescription
                                 );
    }

References and recommended reading

  1. Getting Started with the Client Object Model in SharePoint 2010
  2. Getting Started with Business Connectivity Services in SharePoint 2010

Summary & Download

At this point, I've showed you three short articles in which I describe how you can set up a Business Connectivity Services data source - then utilize the Server OM or Client OM to fetch information from that external data storage.

Worth to note is that this is still a Beta product, which of course means that it may differ in functionality and performance in comparison with the final (RTM) version of SharePoint 2010.

Download the complete Visual Studio 2010 project here: [ Zimmergren.SP2010.ClientOM.BCS.zip ]


Published: Jan-21-10 | 7 Comments | 0 Links to this post