C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Filter SharePoint List Items by Current User or Group
WhatsApp
Kaviya Balasubramanian
9y
19.4
k
0
1
25
Blog
Below is the Query to get the Items,
camlQuery.set_viewXml(
"<View><Query><Where><Or><Membership Type=\"CurrentUserGroups\"><FieldRef Name=\"AssignedTo\"/></Membership><Eq><FieldRef Name=\"AssignedTo\"></FieldRef><Value Type=\"Integer\"><UserID/></Value></Eq></Or></Where></Query></View>"
);
Use the below code to get the items from host web using SharePoint app,
Use strict:
//var context = SP.ClientContext.get_current();
//var user = context.get_web().get_currentUser();
var hostWebURL, appWebURL;
// This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
$(document).ready(function () {
// getUserName();
GetDatafromList();
});
function GetDatafromList() {
hostWebURL = decodeURIComponent(manageQueryStringParameter(
'SPHostUrl'
));
appWebURL = decodeURIComponent(manageQueryStringParameter(
'SPAppWebUrl'
))
var context =
new
SP.ClientContext(appWebURL);
var appCtxSite =
new
SP.AppContextSite(context, hostWebURL);
var web = appCtxSite.get_web();
var list = web.get_lists().getByTitle(‘TestList’);
var camlQuery =
new
SP.CamlQuery();
camlQuery.set_viewXml(
"<View><Query><Where><Or><Membership Type=\"CurrentUserGroups\"><FieldRef Name=\"AssignedTo\"/></Membership><Eq><FieldRef Name=\"AssignedTo\"></FieldRef><Value Type=\"Integer\"><UserID/></Value></Eq></Or></Where></Query></View>"
);
var liscoll = list.getItems(camlQuery);
context.load(liscoll);
context.executeQueryAsync(function () {
console.log(
"My Portfolio"
);
var listItemCount = liscoll.get_count();
if
(listItemCount > 0) {
var enumerator = liscoll.getEnumerator();
while
(enumerator.moveNext()) {
var currentItems = enumerator.get_current();
var title = currentItems.get_item(
"Title"
);
console.log(
"Title is : "
+ title);
}
}
}
, function (sender, args) {
console.log(
"List Items failed"
+ args.get_message());
});
}
function manageQueryStringParameter(paramToRetrieve) {
var
params
= document.URL.split(
"?"
)[1].split(
"&"
);
var strParams =
""
;
for
(var i = 0; i <
params
.length; i = i + 1) {
var singleParam =
params
[i].split(
"="
);
if
(singleParam[0] == paramToRetrieve) {
return
singleParam[1];
}
}
}
Make sure to provide permission for list in appmanifest.xml file as shown in below,
Summary
In this article we have explored how to get the list items for assigned to current user or to the group in which the current user is a member.
Filter
SharePoint
List Items
People also reading
Membership not found