Below is an example of .net windows application application that performs CRUD operations & retrieve site collections users present in group from SharePoint list using .Net managed code.
[code]using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ClientSideApplication
{
public partial class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
private void retrieve_Click(object sender, EventArgs e)
{
//Clear the result list box
ResultListBox.Items.Clear();
var siteUrl = "http://stapvsp9007.gcm.com/sites/RRandCR/";
using (var context = new ClientContext(siteUrl))
{
try
{
var web = context.Web;
var listName = web.Lists.GetByTitle("Year List");
var query = new CamlQuery();
query.ViewXml = "<View>" + "<Query>" + "<OrderBy>" + "<FieldRef Name='ID'/>" + "</OrderBy>" + "</Query>" + "</View>";
var items = listName.GetItems(query);
//Instead of retreving all the information related to list we are load necessary Title information
context.Load(items, c=> c.Include(li => li["Title"]));
context.ExecuteQuery();
foreach (var item in items)
{
ResultListBox.Items.Add(item["Title"]);
}
}
catch (Exception ex)
{
ResultListBox.Items.Add(ex.Message);
}
}
}
private void update_Click(object sender, EventArgs e)
{
ResultListBox.Items.Clear();
var siteUrl = "http://stapvsp9007.gcm.com/sites/RRandCR/";
using (var context = new ClientContext(siteUrl))
{
try
{
var web = context.Web;
var list = web.Lists.GetByTitle("Year List");
var query = new CamlQuery();
query.ViewXml = "<View>" + "<Query/>" + "<RowLimit>1</RowLimit>" + "</View>";
var items = list.GetItems(query);
context.Load(items);
context.ExecuteQuery();
foreach (var item in items)
{
item["Title"] = "2020";
item.Update();
}
context.ExecuteQuery();
ResultListBox.Items.Add("Item Updated");
}
catch (Exception ex)
{
ResultListBox.Items.Add(ex.Message);
}
}
}
private void insert_Click(object sender, EventArgs e)
{
try
{
ResultListBox.Items.Clear();
var siteUrl = "http://stapvsp9007.gcm.com/sites/RRandCR/";
using (var context = new ClientContext(siteUrl))
{
var web = context.Web;
var list = web.Lists.GetByTitle("Year List");
var lic = new ListItemCreationInformation();
var item = list.AddItem(lic);
item["Title"] = "2019";
item.Update();
context.ExecuteQuery();
ResultListBox.Items.Add("Item Added");
}
}
catch (Exception ex)
{
ResultListBox.Items.Add(ex.Message);
}
}
private void delete_Click(object sender, EventArgs e)
{
//Deleting first item from list
ResultListBox.Items.Clear();
var siteUrl = "http://stapvsp9007.gcm.com/sites/RRandCR/";
try
{
using (var context = new ClientContext(siteUrl))
{
var web = context.Web;
var listName = web.Lists.GetByTitle("Year List");
var query = new CamlQuery();
query.ViewXml = "<View>" + "<Query/>" + "<RowLimit>1</RowLimit>" + "</View>";
var items = listName.GetItems(query);
context.Load(items, i => i.Include(li => li["Title"]));
context.ExecuteQuery();
foreach (var item in items)
{
item.DeleteObject();
context.ExecuteQuery();
ResultListBox.Items.Add("Item deleted sucessfully");
}
}
}
catch (Exception ex)
{
ResultListBox.Items.Add(ex.Message);
}
}
private void fetchUsers_Click(object sender, EventArgs e)
{
ResultListBox.Items.Clear();
var siteUrl = "http://stapvsp9007.gcm.com/sites/RRandCR/";
try
{
using (var context = new ClientContext(siteUrl))
{
var web = context.Web;
GroupCollection collGrp = web.SiteGroups;
context.Load(collGrp,
grp => grp.Include(group => group.Users));
context.Load(collGrp);
context.ExecuteQuery();
foreach (Group oGroup in collGrp)
{
ResultListBox.Items.Add("**" + oGroup.Title + "**");
var users = oGroup.Users;
foreach (var user in users)
{
ResultListBox.Items.Add(user.Title);
}
}
}
}
catch (Exception ex)
{
ResultListBox.Items.Add(ex.Message);
}
}
}
}[/code]
No comments:
Post a Comment