Showing posts with label ListItem. Show all posts
Showing posts with label ListItem. Show all posts

Friday, September 7, 2012

How to create folder in sharepoint custom list programmatically


Here is steps to add new item as a folder in sharepoint list programmatically.

- 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 webFolder = site.OpenWeb())
{

- Get folder by folder name in list

SPFolder folder = webFolder.GetFolder("/Lists/Your List Name/Your Folder Name");

- Check whether folder is exist in list or not, if not then create new folder
if (folder.Exists)
{
Response.Write("Folder name already exists.");
return;
}

- Create list and Get County list
SPList list = webFolder.Lists[Your List Name];

- Set AllowUnsafeUpdates to true to insert new item through code
webFolder.AllowUnsafeUpdates = true;

- Create new item to add
SPListItem item = list.Items.Add();
item.FileSystemObjectType = SPFileSystemObjectType.Folder;

- Set values for the folder name
item["Title"] = “Your Folder Name”;

- fire update event for County list
item.Update();
webFolder.Update();

 }
}

-That’s it for adding new item to your sharepoint list as folder.

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.

Thursday, May 31, 2012

Delete sharepoint list item programmatically

- Create new instance of site with your current application
using (SPSite site = new SPSite(SPContext.Current.Web.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)
{

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

- Delete selected item from fetched list
common[0].Delete();

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

}
}


Tuesday, May 29, 2012

Update existing sharepoint list item programmatically


Here is steps to update existing sharepoint list item 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)
{

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


- Get first list item from the result list
SPListItem item = common[0];

- Set new value for the column which we want to update
item["Title"] = “New Value”;

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

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

Monday, May 28, 2012

Insert new item to sharepoint list programmatically


Here is steps to add new item to sharepoint list programmatically.
- Declare variable which hold the newly created item id.
int _Int_ID = 0;

- 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 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";
- 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;
        }
}

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