none
how to get list item attachments using client object model ecma lib

    Question

  • Hi,

     I'd like to know how to get the attachments paths of a list item using the client object model ecma library.

     

            context = new SP.ClientContext.get_current(); 

            web = context.get_web(); 
            var list = web.get_lists().getByTitle('ListName');
            itemToShow = list.getItemById(ItemID);          
           
            context.load(itemToShow, 'Title','Attachments');
            context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

    The attachments field contains TRUE if the item has attachments or FALSE if not, but what I need is the path of the attachments..

     

    Thanks.

    • Moved by Wayne Fan Friday, August 27, 2010 8:21 AM (From:SharePoint 2010 - Using SharePoint Designer, Infopath, and other customization)
    Wednesday, August 25, 2010 12:53 PM

Answers

  • Hi  Ghiluz,

     

    Thanks for your post.

     

    You can get the path using “SP.Folder” object instead of “SP.ListItem”. The attachment files are stored in the list as the structure as follows:

    List rootfoler à ‘attachment’ folder à ‘Item Id’ folder à attachments.

    For example, the path of the attachment in my server  likes this: http://<servername>/lists/listtest/Attachments/1/doctext.docx.

     

    So the main logic of my solution is:

    1.       Get the folder of the url likes : var attachmentFolder=web.getFolderByServerRelativeUrl('Lists/City/Attachments/'+itemId);

    2.       Loop all the files in the folder, and get the path of them using get_serverRelativeUrl;

    Here is my sample code, hope it’s helpful.

    <script type="text/javascript">

     

       ExecuteOrDelayUntilScriptLoaded(getWebProperties, "SP.js");

                   

       var attachmentFiles;

    function getWebProperties() {

    var itemId=2;

    var ctx = new SP.ClientContext.get_current();

    var web = ctx.get_web();

    var attachmentFolder=web.getFolderByServerRelativeUrl('Lists/City/Attachments/'+itemId);

       attachmentFiles= attachmentFolder.get_files();

       ctx.load(attachmentFiles);

                   

            ctx.executeQueryAsync(Function.createDelegate(this,this.onSuccess),Function.createDelegate(this,this.onFailed));

    }

     

        function onSuccess(sender, args) {                                     

                                    var i=0;

                                    for(var file in attachmentFiles)

                                    {                             

                                                    alert(attachmentFiles.itemAt(i).get_serverRelativeUrl());

                                                    i++;

                                    }

        }

     

        function onFailed(sender, args) {

            alert("sorry!");

        }

    </script>

    • Marked as answer by Ghiluz Monday, August 30, 2010 10:00 AM
    Saturday, August 28, 2010 10:50 AM

All replies

  • For development questions, please use the development forum.  Thanks!
    SharePoint Architect || Microsoft MVP || My Blog
    Planet Technologies || SharePoint Task Force
    Wednesday, August 25, 2010 4:25 PM
  • Hi  Ghiluz,

     

    Thanks for your post.

     

    You can get the path using “SP.Folder” object instead of “SP.ListItem”. The attachment files are stored in the list as the structure as follows:

    List rootfoler à ‘attachment’ folder à ‘Item Id’ folder à attachments.

    For example, the path of the attachment in my server  likes this: http://<servername>/lists/listtest/Attachments/1/doctext.docx.

     

    So the main logic of my solution is:

    1.       Get the folder of the url likes : var attachmentFolder=web.getFolderByServerRelativeUrl('Lists/City/Attachments/'+itemId);

    2.       Loop all the files in the folder, and get the path of them using get_serverRelativeUrl;

    Here is my sample code, hope it’s helpful.

    <script type="text/javascript">

     

       ExecuteOrDelayUntilScriptLoaded(getWebProperties, "SP.js");

                   

       var attachmentFiles;

    function getWebProperties() {

    var itemId=2;

    var ctx = new SP.ClientContext.get_current();

    var web = ctx.get_web();

    var attachmentFolder=web.getFolderByServerRelativeUrl('Lists/City/Attachments/'+itemId);

       attachmentFiles= attachmentFolder.get_files();

       ctx.load(attachmentFiles);

                   

            ctx.executeQueryAsync(Function.createDelegate(this,this.onSuccess),Function.createDelegate(this,this.onFailed));

    }

     

        function onSuccess(sender, args) {                                     

                                    var i=0;

                                    for(var file in attachmentFiles)

                                    {                             

                                                    alert(attachmentFiles.itemAt(i).get_serverRelativeUrl());

                                                    i++;

                                    }

        }

     

        function onFailed(sender, args) {

            alert("sorry!");

        }

    </script>

    • Marked as answer by Ghiluz Monday, August 30, 2010 10:00 AM
    Saturday, August 28, 2010 10:50 AM
  • Hi, it works great!!

    thanks a lot

     

    Monday, August 30, 2010 9:59 AM
  • it worked in IE but not in FF. Is there any reason behind this???

    Tuesday, June 12, 2012 9:29 PM
  • The below link will clearly explain "how to read list item attachments using client object model

    http://vangalvenkat.blogspot.com/2011/08/sharepoint-2010-getting-list-item.html 

    Thursday, June 21, 2012 10:11 AM