Results 1 to 3 of 3

Thread: Adding an Attachment to a List Item Programmatically in SharePoint

  1. #1
    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

  2. #2
    Join Date
    May 2008
    Posts
    4,088

    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;

  3. #3
    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.

Similar Threads

  1. Adding SharePoint server on existing farm
    By Gopal Shrivastav in forum Networking & Security
    Replies: 4
    Last Post: 28-04-2012, 03:49 AM
  2. Sharepoint Tips for adding items in List
    By Leoniee in forum Tips & Tweaks
    Replies: 2
    Last Post: 01-02-2011, 06:54 AM
  3. Replies: 3
    Last Post: 12-11-2010, 02:38 AM
  4. Update vs. SystemUpdate for SharePoint List Item
    By bijayani in forum Software Development
    Replies: 1
    Last Post: 23-06-2010, 12:04 AM
  5. Replies: 2
    Last Post: 05-06-2009, 12:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •