Saturday, June 30, 2012

Delete sharepoint list item attachment programmatically




Here is steps to delete sharepoint list item attachment programmatically.

- Follow the steps to delete list item attachment from your sharepoint list.

- Create new instance of site with your current application
using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
{

- Create new web instance and open web for current application
using (SPWeb web = site.OpenWeb())
{

- Create new list object and get your list
SPList SPLData = web.Lists["Your List Name"];

- Create new query object to hold query which fetches list item of the list
SPQuery SPMyQuery = new SPQuery();

- Assign query with value to it in ID field of the list item

SPMyQuery.Query = string.Format(“<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>{0}</Value></Eq></Where>”,1);

//========== Fetch item collection data for Your List  ==========//
SPListItemCollection common = SPLData.GetItems(SPMyQuery);

SPListItem listItem = common[0];

if (listItem.Attachments.Count > 0)
{
listItem.Attachments.Delete(“Name of file”);
listItem.Update();
}

}

}

-That’s it for deleting attachments from your existing list item of sharepoint list.

No comments:

Post a Comment