Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Thursday, December 01, 2011

Delete in the SharePoint List using Client Object Model within a WebPart.

try
{
SPWeb currentWeb = SPContext.Current.Web;
ClientContext clientContext = new ClientContext(currentWeb.Url);
Web web = clientContext.Web;
List list = web.Lists.GetByTitle("Login");
CamlQuery query = new CamlQuery();
query.ViewXml = @"<Query><Where><Eq><FieldRef Name='ID' /><Value
Type='Counter'>" + ddlID.SelectedItem.Value + "</Value></Eq></Where></
Query>";
Microsoft.SharePoint.Client.ListItemCollection listItems =
list.GetItems(query);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.ListItem item in listItems)
{
if (ddlID.SelectedItem.Value == item["ID"].ToString())
{
item.DeleteObject();
//item.Update();
break;

}
}
clientContext.ExecuteQuery();
//string strRedirect = currentWeb.Url + "/UserPages/Updation Using
Com.aspx";
//Page.Server.Transfer(strRedirect);
ddlID.Items.Clear();
Microsoft.SharePoint.Client.ClientContext ctx = new
Microsoft.SharePoint.Client.ClientContext(currentWeb.Url);
Microsoft.SharePoint.Client.List lstLogin =
ctx.Web.Lists.GetByTitle("Login");
Microsoft.SharePoint.Client.CamlQuery sQuery =
Microsoft.SharePoint.Client.CamlQuery.CreateAllItemsQuery();
sQuery.ViewXml = @"<View><Query><OrderBy><FieldRef Name='ID'
Ascending='False' /></OrderBy></Query></View>";
Microsoft.SharePoint.Client.ListItemCollection myColl =
lstLogin.GetItems(sQuery);
ctx.Load(myColl);
ctx.ExecuteQuery();
if (myColl.Count > 0)
{
foreach (Microsoft.SharePoint.Client.ListItem item in myColl)
{
ddlID.Items.Add(item["ID"].ToString());
}
ctx.Load(lstLogin.GetItems(sQuery));
}
ddlID.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Select
ID"));
strError += "Deletion Successful";
txtUserName.Text = "";
txtPassWord.Text = "";
}
catch (Exception ex)
{
strError += ex.ToString();
}

No comments:

Post a Comment