4

I'm retrieving items from a list. The list contains entries which have attachments. Can I modify my query to include the attachment URLs? What's the most efficient way of doing this?

<View>
<Query>
<Where><Eq><FieldRef Name='Special' /><Value Type='Boolean'>1</Value></Eq></Where>
<OrderBy><FieldRef Name='News_x0020_Date' Ascending='FALSE' /></OrderBy>
</Query>
<RowLimit>5</RowLimit>
</View>
2

IncludeAttachmentURLs is not available with CAML query object of .NET Client object model. To retrieve the files from the attachment field you have to try the following:

Folder folder = clientContext.Web.GetFolderByServerRelativeUrl(
spList.RootFolder.ServerRelativeUrl + "/Attachments/" + item.Id);    
FileCollection files = attFolder.Files;    
// If you only need the URLs    
ctx.Load(files, fs => fs.Include(f => f.ServerRelativeUrl));    
ctx.ExecuteQuery();

The attachments are not stored in the list item but stored in a sub folder named "Attachments" and if this item has one or more attachments then a folder is created with the name as Id of the item where all the attachments for that item are stored.So , in the above code we are retrieving all the items for the folder which has the attachments

Reference

  • I would have to loop this for every result from my original CAML query right? I'm hoping to do something more efficient. – matt Nov 21 '14 at 17:44
  • IncludeAttachmentURL will work if you are using javascript – Harshini Hegde Nov 21 '14 at 17:46
  • The problem is I have to use CAML to get the default label value for my managed meta data column. – matt Nov 21 '14 at 17:49
0

The "Include attachment urls" section of this article may provide you help with what you want to achieve.

Let us know if it helped.

  • I've seen this page, but I can't get it to work. – matt Nov 20 '14 at 23:46
  • Were were you stuck ? – Amit Tyagi Nov 20 '14 at 23:52
  • I should clarify that I'm using JavaScript (SP.CamlQuery). I don't know how to modify my CAML to include the attachment URLS. Nothing I've tried works. – matt Nov 21 '14 at 0:14
  • Hmmm.....it looks like this(IncludeAttachmentURLs) option works with the server side Object Model only. – Amit Tyagi Nov 21 '14 at 0:38
  • Check if this(jbmurphy.com/2010/10/28/… ) helps. – Amit Tyagi Nov 21 '14 at 0:56
0

Although the IncludeAttachmentURLs option is really not available via the client object model (at least in version 2013), you can still use the "good-old" SharePoint Web Services to submit a CAML query including this option if you need to work from the client side, as described here:

$(document).ready(function() {
querySPWebServices();
});

function querySPWebServices() {
        var soapEnv = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
        <soapenv:Body> \
        <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
        <listName>Projects</listName> \
        <viewFields> \
        <ViewFields> \
        <FieldRef Name='Title' /> \
        <FieldRef Name='Body' /> \
        <FieldRef Name='ID' /> \
        <FieldRef Name='Attachments' /> \
        </ViewFields> \
        </viewFields> \
        <query> \
        <Query /> \
        </query> \
        <queryOptions> \
        <QueryOptions> \
        <IncludeAttachmentUrls>TRUE</IncludeAttachmentUrls> \
        </QueryOptions> \
        </queryOptions> \
        </GetListItems> \
        </soapenv:Body> \
        </soapenv:Envelope>";

        $.ajax({
            async: false,
            url: "http://server.name.com/site/_vti_bin/lists.asmx",
            type: "POST",
            dataType: "xml",
            data: soapEnv,
            complete: processResult,
            contentType: "text/xml; charset=\"utf-8\""
            });
};
function processResult(xData, status) {
    $(xData.responseXML).find("z\\:row").each(function() {
        if ($(this).attr("ows_Attachments") != 0) {
        var url = $(this).attr("ows_Attachments").replace(/#/g, "").split(';');
        var ID = $(this).attr("ows_ID");
        $("table.ms-listviewtable tr:has(td.ms-vb2)").find("td:first").filter(function() {
            return $(this).text().toLowerCase() == ID;
            }).html("<img src='" + url[1] + "' width=150 height=100 />");
        };
    });
};

Or you can use REST (as the title of your question suggest):

http://YourSharePoint/SubWeb/_api/Web/Lists/GetByTitle('YourList')/Items/?$expand=AttachmentFiles

Instead of CAML, add the condition, ordering and row limit attributes of your query via $filter, $orderby and $top to the REST query as described here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.