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

  • 问题

  • 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..

    2010年8月25日 12:54

答案

  • Here the solution:

     

    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 ;


    <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>

    2010年8月30日 10:00

全部回复

  • I haven't tested it yet, but I've found this post http://stackoverflow.com/questions/2969810/sharepoint-2010-client-object-model-add-attachment-to-listitem

    It looks like the attachments are in a sub-folder; I just hope that this will put you on the right track.


    Serge Luca; MVP blog: http://sergeluca.spaces.live.com Devoteam Belgium. http://twitter.com/sergeluca
    2010年8月25日 19:46
  • Here's how it's done in the Server OM:

    http://www.binarywave.com/blogs/eshupps/Lists/Posts/Post.aspx?List=9c93c708%2De5ce%2D4714%2Dbdea%2D499330361130&ID=26

    I haven't tried this yet in the Client OM but you should be able to get the attachments once you have the list item.

    2010年8月27日 13:44
  • Here the solution:

     

    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 ;


    <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>

    2010年8月30日 10:00
  • But, how to download these attachments using the javascript code?
    2018年4月13日 7:12