Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharePoint 2010 REST filter by current user

Using REST in SharePoint 2010, how do you select / filter rows of a list which belong to the user currently logged in (whether it's a case of filtering by Author, Editor, or by a Person field)

e.g.

http://myserver/_vti_bin/ListData.svc/Test?$filter=Author eq Me

I know in SP2007, using caml you can use the UserID element which represents the value of the current user.

EDIT: Humm, seems the Author, Editor and any other Person field are stored as Ints and are renamed to CreatedById and ModifiedById. If you query a list all you get back is

<d:CreatedById m:type="Edm.Int32">1</d:CreatedById>

What if I want to get back the NT name of the user? Do I have to do another query somewhere to get the Int value for a certain user?

like image 807
Fergal Avatar asked Feb 18 '23 20:02

Fergal


1 Answers

You can also use this with UserID. Retrieve UserId by using code:

var userId = _spPageContextInfo.userId;

and use it in URL as below:

"http://spdevportal/dev/Lab2/_vti_bin/ListData.svc/IPParams?$filter=CreatedById eq"+userId

Ex: http://spdevportal/dev/Lab2/_vti_bin/ListData.svc/IPParams?$filter=CreatedById eq 213

It will give you all the items created by Currently LoggedIn user.

You can also add some additional filter using 'and'

"http://spdevportal/dev/Lab2/_vti_bin/ListData.svc/IPParams?$filter=CreatedById eq"+userId+" and StatusValue eq 'approved'"

Ex:

"http://spdevportal/dev/Lab2/_vti_bin/ListData.svc/IPParams?$filter=CreatedById eq 213 and  StatusValue eq 'approved'"
like image 110
Amol Eklare Avatar answered Feb 28 '23 16:02

Amol Eklare