1

Hi I need to get all user name who is celebrating birthday today based on the value in SPS-Birthday field using Rest API?

How to achieve this?

sherin
  • 159
  • 2
  • 4
  • 15
  • $.ajax({ url: siteUrl +"/_api/search/query?querytext='*'&sourceid='B09A7990-05EA-4AF9-81EF-EDFAB16C4E31'&rowlimit=5&selectproperties='PreferredName,WorkEmail,PictureUrl,Title,Departament,RefinableDate00,Url'&refinementfilters='RefinableDate00:datetime("2000-02-08")'",
    headers: { Accept: "application/json;odata=verbose" },
    });
    
    – sherin Feb 09 '17 at 04:34

2 Answers2

2

I think direct it is not possible what you will need first fetch all site users using below REST URL.

Your Site URL + "/_api/web/siteusers"

Then get account name for all users from above response and generate string like below:

var targerUsers = ["i:0#.f|membership|test.onmicrosoft.com","i:0#.f|membership|dtest1.onmicrosoft.com"];

Then pass this variable in below URL:

http://siteurl/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='SPS-Birthday')?@v=targerUsers

Then in response of above query result filter by your requirement.

Ronak Patel
  • 3,261
  • 3
  • 23
  • 45
Samir Khimani
  • 2,233
  • 11
  • 16
1

You can use in conjunction with SharePoint Search REST API as below:

https://sitecollectionurl/_api/search/query?querytext='*'&sourceid='B09A7990-05EA-4AF9-81EF-EDFAB16C4E31'&rowlimit=5&selectproperties='PreferredName,WorkEmail,PictureUrl,Title,Department,RefinableDate00,Url'&refinementfilters='RefinableDate00:datetime("2000-02-08")'

Here , source id = guid of the people data source

You need to map the property SPS-Birthday to one the Refinable properties like RefinableDate00. Check below image.

enter image description here

Once done, wait for the crawl to be completed. In SPO, it takes anything between 24 hours to 1 week ( yes 1 freaking week).

Also important thing to note, the Date is stored something like yyyy-mm-dd. Here, yyyy is always the year 2000.

So in the above query you must form a date from today and then append the year 2000 to it.

&refinementfilters='RefinableDate00:datetime("2000-02-08")'

Update as per Danny's comment

So today's value can be fetched as below:

String.format("{0:yyyy}-{0:MM}-{0:dd}",new Date(new Date().setFullYear(2000)) );

Pass this value to the REST api and after that process the JSON results :)

Gautam Sheth
  • 30,881
  • 1
  • 35
  • 62
  • 1
    Why? use JavaScript hacks. SharePoint provides a String.format function String.format("{0:yyyy}-{0:MM}-{0:dd}",new Date()); http://sharepoint.stackexchange.com/questions/160806/changing-date-format-using-javascript – Danny '365CSI' Engelman Feb 08 '17 at 13:39
  • 1
    To set to a fixed year you have to jump through an extra hoop (because Microsofts .format( ) does not accept milliseconds): String.format("{0:yyyy}-{0:MM}-{0:dd}",new Date(new Date().setFullYear(2000)) ); – Danny '365CSI' Engelman Feb 08 '17 at 13:42
  • @Danny'365CSI'Engelman - this is awesome ! thanks a lot. Off topic, is there any way i can pass the culture value ? I usually use momentjs for that, would be awesome if its doable using this way. – Gautam Sheth Feb 08 '17 at 15:01
  • Dunno, they say Its America First and The Netherlands second, so (I think) I only should use my own culture ... is probably somewhere in the page context info ? – Danny '365CSI' Engelman Feb 08 '17 at 15:18
  • hey guys, the query is throwing bad request – sherin Feb 09 '17 at 04:32
  • @sherin - the query works for me. Can you try hitting the query in your browser and check ? Ensure that &refinementfilters='RefinableDate00:datetime("2000-02-08")' has the correct quotes. Also note that it takes some time for the Refinable properties to get populated. – Gautam Sheth Feb 09 '17 at 10:53