3

Resorting to asking a question on here as I cant seem to find anyone else who is trying to access the Target Audiences field with the REST API.

If I try and pull out all the items in my list using:

/_api/lists/getbytitle('xxxx')/items

The definition returned doesnt contain the Target Audiences field, even though its part of the content type associated with my list and I've added an item to the list and populated this field. If I try and grab this specific field:

/_api/lists/getbytitle('xxxx')/items?$select=Audience

It says The field or property 'Audience' does not exist. Even though it is there on the content type and list.

I chucked in a quick bit of server side code on page load and I can get the field using server side code:

SPWeb web = SPContext.Current.Web;
        SPList list = web.Lists.TryGetList(xxxx);
        if (list != null)
        {
            foreach (SPListItem item in list.Items)
            {
                var audiences = item["Target Audiences"];
            }
        }

But does anyone know why it isnt accessible with client side code? I'm creating a custom admin screen for a list which has a Target Audience field so I need to be able to pull the value back and set the value as well, but it's like the client side doesnt think it exists, when it clearly does and can be accessed on the server side

  • Target Audiences is 9/10 stored in a different list and thus the call wont recieve it same as user id ect, Btw shouldnt your call be /_api/lists/getbytitle('xxxx')/items?$select='Target Audiences' Or am i looking at this wrong? – Raoul Mensink Apr 13 '16 at 7:05
1

It is not supported by REST api, you need caml query to pull items based on Target Audience. However you can use caml based REST call to get items based on Target Audiences. Here i am providing an example where i am making a REST call to get items based on current user Targeted Items. REST calls has the following inabilty.

1.Inability to filter items based on user fields where user is added through a group, rather than directly, e.g. AssignedTo=[Me] combined with a SharePoint group.

2.Inability to filter items based on multivalued taxonomy fields.

var caml = "<View><Query><Where><Or><Eq><FieldRef Name='AssignedTo' /><Value Type='Integer'><UserID/></Value></Eq><Membership Type='CurrentUserGroups'><FieldRef Name='AssignedTo' /> </Membership></Or></Where></Query></View>";

var endpoint = "/_api/web/lists/GetByTitle('" 
    + listName + "')/GetItems";
var requestData = { "query" :
       {"__metadata": 
          { "type": "SP.CamlQuery" }
          , "ViewXml": caml
       }
    };
return jQuery.ajax({
    url: endpoint,
    method: "POST",
    data: requestData,
    headers: {
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "Accept": "application/json; odata=verbose",
        "Content-Type": "application/json; odata=verbose"
    }
});
/* end of code */
  • in short. Is target Audience filter can be applied in REST call in SharePoint? I am confused. If yes Which column in above example is target audience? any response is appreciated ! – Varun Verma Aug 7 '16 at 16:45
  • Yes it can be done, see the code i posted. You can only achieve it using CAML query and when you make a CAML query request it is a POST request. "AssignedTo" is the target audience column. – Akarsh Gupta Aug 26 '16 at 3:59
  • @AkarshGupta I tried your example, it retrieves all items instead of targeted items. Can you please provide a even better example using JSOM. – Unknown Nov 25 '16 at 10:22
  • In JSOM you can again create your caml query and retrieve items based on target items – Akarsh Gupta Nov 28 '16 at 1:55
  • You mean, we have to provide a particular audience's GUID and write a CAML query to filter out? If yes, then how would we know that Current user satisfies which audience and its GUID. If am wrong then please elaborate your answer. Currently am in need of this feature in add-in. – Unknown Nov 28 '16 at 5:33
0

Not so much an answer to the original problem but I dont believe its possible to do what I was trying to do (access Target Audience on the client side).

So if anyone else has this problem avoid the headache of trying to figure it out and do it server side (which is what I did in the end). I changed my REST API calls to custom web service calls.

To then render my items with Audience Targetting taken into account, if you aren't aware (as I wasnt) you use the AudienceLoader:

AudienceLoader audienceLoader = AudienceLoader.GetAudienceLoader();

            foreach (SPListItem item in listItems)
            {
                string audienceFieldValue = (string)item["Target Audiences"];

                if (AudienceManager.IsCurrentUserInAudienceOf(audienceLoader, audienceFieldValue, true))
                {
                }
             }

I stole this from another stack overflow I believe.

If anyone does ever come across a Target Audience solution client side I'd be interested though.

  • It is possible, you need to make a REST call in which you need to POST your CAML query. In the CAML you can filter the Target Audience, See my post above. – Akarsh Gupta Aug 26 '16 at 4:02

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.