Tuesday, January 31, 2012

Build view List item url and Edit List item url-sharepoint 2010



In this post we are going to programatically build view List item url and Edit List item url-sharepoint 2010.
Below is how the url's supposed to be .

View list item url::
http://<site url>/Lists/<ListName>/DispForm.aspx?ID=<ItemID>&Source=/Lists/<ListName>

Edit list item url::
http://<site url>/Lists/<ListName>/EditForm.aspx?ID=<ItemID>&Source=/Lists/<ListName>



Hope this helps...

Thursday, January 12, 2012

How to filter a DataTable using LINQ

Hi Friends,

In this post we are going to Filter a DataTable using LINQ.

We can filter a DataTable in many ways but using LINQ is the best and recommended approach..
In the below code snippet we are going to filter the datatable and then assign the results back to it.

Below are the assumptions before you jump into the code.
  • dtMainTable is the DataTable which you got it from your logic.

var tempTable = from DataRow dataRow in dtMainTable.Rows
                                    where dataRow["ColumnName"] == "Value"
                                    select dataRow;
                    if (tempTable.Count() > 0)
                    {
                        dtMainTable= tempTable.CopyToDataTable();
                        dtMainTable.AcceptChanges();
                    }

Hope this helps...

Wednesday, December 28, 2011

Delete multiple list items from a sharepoint list using Object modeling


In the below code snippet we are going to Delete multiple list items (which returned from a CAML query) from a sharepoint list using Object modeling.

Assumptions::
listItemFieldID  --> List item ID internal field
listItemFieldName --> List item field which you will be querying.
 listItemName --> value of the field

Code::
SPSite ospSite = null;
            SPWeb ospWeb = null;
            SPList ospList = null;
            SPListItemCollection spListItems = null;
            string camlViewQuery = string.Empty,
                           camlFilterQuery = string.Empty,
                           listItemFieldID = "ID";

                //view fields query depends on your requirement
                camlViewQuery = "<FieldRef Name='" + listItemFieldID + "' /><FieldRef Name='" + listItemFieldName + "' />";

                //filter query depends on your requirement
                camlFilterQuery = "<Where><Eq><FieldRef Name='" + listItemFieldName + "'/><Value Type='Lookup'>" + listItemName + "</Value></Eq></Where>";

                using (ospSite = new SPSite(siteUrl))
                {
                    using (ospWeb = ospSite.OpenWeb())
                    {
                        try
                        {
                            ospWeb.AllowUnsafeUpdates = true;
                            ospList = ospWeb.Lists.TryGetList(listName);

                            if (ospList != null && ospList.ItemCount > 0)
                            {
                                SPQuery query = new SPQuery()
                                {
                                    //query the list.
                                    Query = camlFilterQuery,
                                    ViewFields = camlViewQuery,
                                    ViewFieldsOnly = true,
                                };

                                spListItems = ospList.GetItems(query);
                                if (spListItems!= null)
                                {
                                    foreach (SPListItem listItem in spListItems)
                                    {
                                        ospList.Items.GetItemById(Convert.ToInt32(listItem["ID"])).Delete();
                                    }

                                    
                                }
                                ospList.Update();
                                ospWeb.Update();
                                ospWeb.AllowUnsafeUpdates = false;

                            }


                        }
                        catch (Exception ex)
                        {

                        }
                    }
                }

           
 Hope this helps..

Popular Posts