Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Adding an Attachment to a List Item Programmatically in SharePoint

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 23-06-2010
Member
 
Join Date: Jun 2010
Posts: 7
Adding an Attachment to a List Item Programmatically in SharePoint
  

As we can have multiple attachments against a list item, it has always been a requirement to fetch these attachments and get it displayed within a custom webpage.

Below is code snippet in c#.net for fetching all the attachements for a specific list item.
try
{
// Set the Site Url
SPSite objSite = new SPSite("http://Testsite/");

using (SPWeb objWeb = objSite.OpenWeb())
{
objWeb.AllowUnsafeUpdates = true;

// Get the List
SPList objList = objWeb.Lists["MyList"];

// Get the item by ID
SPListItem objItem = objList.GetItemById(1);

// Get the attachments of the item
SPAttachmentCollection objAttchments = objItem.Attachments;

Thanks

Reply With Quote
  #2  
Old 24-06-2010
Member
 
Join Date: May 2008
Posts: 4,080
Re: Adding an Attachment to a List Item Programmatically in SharePoint

Do you know how to add docx attachment to list programmatically? I have gone through the below code snippet in c#.net for fetching all the attachements for a specific list item which I think isnt correct, so can you let me know about same:

Code:
{
// Set the Site Url
SPSite objSite = new SPSite("http://Testsite/");

using (SPWeb objWeb = objSite.OpenWeb())
{
objWeb.AllowUnsafeUpdates = true;

// Get the List
SPList objList = objWeb.Lists["MyList"];

// Get the item by ID
SPListItem objItem = objList.GetItemById(1);

// Get the attachments of the item
SPAttachmentCollection objAttchments = objItem.Attachments;
Reply With Quote
  #3  
Old 30-06-2010
Member
 
Join Date: Jun 2010
Posts: 7
Re: Adding an Attachment to a List Item Programmatically in SharePoint

In case you need to do the upload the file as an attachment to a custom list using the object model, the approach is slightly different. Adding a file to the list item can be done by accessing the Attachments collection of the SPListIem.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Adding an Attachment to a List Item Programmatically in SharePoint"
Thread Thread Starter Forum Replies Last Post
Adding SharePoint server on existing farm Gopal Shrivastav Networking & Security 4 28-04-2012 04:49 AM
Sharepoint Tips for adding items in List Leoniee Tips & Tweaks 2 01-02-2011 07:54 AM
Adding new Item to “Add New Item” Dialog in Visual Studio 2010 NProtector Windows Software 3 12-11-2010 03:38 AM
Update vs. SystemUpdate for SharePoint List Item bijayani Software Development 1 23-06-2010 01:04 AM
asp.net adding databound hyperlink columns to gridview programmatically Amitesh Software Development 2 05-06-2009 01:02 AM


All times are GMT +5.5. The time now is 03:21 AM.