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.

Wednesday, June 13, 2012

Get sharepoint list item attachment programmatically


Here is steps to get sharepoint list item attachment programmatically.

- Follow the steps to insert new item into 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);

- Assign query to list and get item on the basis of the query build above
SPListItemCollection common = SPLData.GetItems(SPMyQuery);

- Check whether list collection contains result of the query, if contains item the proceed to update value of the list item
if (common.Count > 0)
{

SPListItem mylistItem = common[0];
string[] _Str_Attachments = GetAttachmentCollection(mylistItem);

- You can assign attachment collection with grid and if you want to give link to the attachment filename, on each file name with anchor link put following code for href of anchor.
string.Format("/Lists/{0}/Attachments/{1}/{2}", "Your List Name”,1,”FileName”);

}
}
}

- Get sharepoint list item attachment collection
public string[] GetAttachmentCollection(SPListItem item)
{
string FileNames = String.Empty;
if (item.Attachments.Count > 0)
{
string[] _Str_File_Name = new string[item.Attachments.Count];
for (int i = 0; i < item.Attachments.Count; i++)
{
SPFile file = item.ParentList.ParentWeb.GetFile(item.Attachments.UrlPrefix + item.Attachments[i].ToString());
_Str_File_Name[i]= file.Name;
}
return _Str_File_Name;
}
return null;
}



-That’s it for fetching attachments for existing item of sharepoint list.

Saturday, June 2, 2012

Insert new item with attachment to sharepoint list programmatically


Here is steps to add new item with attachment to sharepoint list programmatically.

- Get name of the file
string _Str_File_Name = fuAttachment.PostedFile.FileName;

- Get stream data by following way
byte[] contents = GetFileStream();

- Declare variable which hold the newly created item id.
int _Int_ID = 0;

- Follow the steps to insert new item into your sharepoint list with attachment.

- 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 list = web.Lists["Your List Name"];

- Set allowunsafeupdates for web & site to true
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;

- Create new list item object and add new item for newly created list object
SPListItem item = list.Items.Add();

- Assign value for newly created item
item["Title"] = "Title1";

- Assign selected filename and stream data
if (!string.IsNullOrEmpty(_Str_File_Name != null) && (contents.Length > 0))
{
SPAttachmentCollection fileAttach = item.Attachments;
fileAttach.Add(_Str_File_Name, contents );
}


- Update item which will add new item to list
item.Update();

- if you want to manipulate on newly created item then you can get id of the newly created item by following way
itemId = item.ID;
}
}

- Convert your selected file to byte by following function
private byte[] GetFileStream()
{
//fuAttachment is your file select control
if (!fuAttachment.HasFile)
return null;
Stream fStream = fuAttachment.PostedFile.InputStream;
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
return contents;
}
Click Insert new item to sharepoint list programmatically to view my previous post to add item to sharepoint list.

-That’s it for adding new item to your sharepoint list with attachment.