Get sharepoint document library programmatically using Client Object Model :
Get sharepoint document library programmatically using Client Object Model :
Hi here i will explain how to get sharepoint Document Library details using Silverlight. Here i get the document Library details in a C# custom list, and display that details in a combo box.
OutPut Screen :
Design Page :
Xaml Code :
Use the following code between User Control<Grid x:Name="LayoutRoot" Background="White">
<Button Content="Button" x:Name="btn" Height="39" Width="64" Click="btn_Click" Canvas.Left="6" Canvas.Top="96" Margin="12,41,1124,619" />
<ComboBox Height="38" HorizontalAlignment="Left" Margin="103,42,0,0" Name="comboBox1" VerticalAlignment="Top" Width="568" />
</Grid>C# Code :
using System;using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;
namespace LibraryData
{
public partial class MainPage : UserControl
{
ContextMenu site;
List LibraryName;
ListItemCollection _icollLibrary;
{
public partial class MainPage : UserControl
{
ContextMenu site;
List LibraryName;
ListItemCollection _icollLibrary;
//this is a custom list used to store Document details
int FileCounter = 0; //this counter is used to get next count of file inside theaddFile function
//Here we are loading site and Sharepoint Library=======================
public MainPage()
{
InitializeComponent();
site.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), null);
}
private void success(object sender, ClientRequestSucceededEventArgs arg)
{
Dispatcher.BeginInvoke(GetFiles);
}
List<DocumentData> lobjDocFiles;
public MainPage()
{
InitializeComponent();
site = new ClientContext("your site url");
site.Load(site.Web);
LibraryName = site.Web.Lists.GetByTitle("Library Name");
site.Load(LibraryName);
site.Load(LibraryName.RootFolder);
site.Load(LibraryName.RootFolder.Files);
CamlQuery qry = new CamlQuery();
qry.ViewXml = "<view/>";
_icollLibrary = LibraryName.GetItems(qry);
site.Load(_icollLibrary);
}
private void success(object sender, ClientRequestSucceededEventArgs arg)
{
Dispatcher.BeginInvoke(GetFiles);
}
//get all files Url and name from liberary(from the following 3 functions)==============
void GetFiles()
{
lobjDocFiles = new List<DocumentData>(); //create instance for custom list
foreach (Microsoft.SharePoint.Client.File doc in LibraryName.RootFolder.Files)
{
site.Load(doc);
site.ExecuteQueryAsync(addFiletoUI, null); // We will move to loadSiteData method on success
}
}
void addFiletoUI(Object sender, ClientRequestSucceededEventArgs e)
{
Dispatcher.BeginInvoke(addFile);
}
void addFile()
{
string fname = LibraryName.RootFolder.Files[FileCounter].Name;
string fUrl = LibraryName.RootFolder.Files[FileCounter].ServerRelativeUrl;
FileCounter++;
//adding file details to the custom list here
lobjDocFiles.Add(new DocumentData { FileName = fname, FileUrl = fUrl });
}
//load file detail to combobox=======================================
private void btn_Click(object sender, RoutedEventArgs e)
{
foreach (var item in lobjDocFiles)
{
comboBox1.Items.Add(item.FileUrl);
}
}
}
//class for custom list that contains File Details
public class DocumentData
{
public string FileName { get; set; }
public string FileUrl { get; set; }
}
}
Comments
Post a Comment