0

I am currently trying to write a OData query to determine if the current user belongs to a particular SharePoint group on SharePoint 2019 Server.

Solution #1: Retrieve the user's groups, then determine if group is part of user properties URL: https://test.com/_api/Web/CurrentUser/Groups

Here is the XML generated (I have removed the other nodes I do not need from the sample below)

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="https://test.com">
    <id>123213123</id>
    <entry>
        <id>https://test.com</id>
        <content type="application/xml">
            <m:properties>
                <d:LoginName>Visitors</d:LoginName>
                <d:Title>Visitors</d:Title>
            </m:properties>
        </content>
    </entry>
        <entry>
        <id>https://test.com</id>
        <content type="application/xml">
            <m:properties>
                <d:LoginName>Visitors 2</d:LoginName>
                <d:Title>Visitors 2</d:Title>
            </m:properties>
        </content>
    </entry>
</feed>

Solution #2: Get SharePoint group and determine if user is in the group.

URL: https://test.com/_api/Web/SiteGroups/GetByName('Visitors 2')

Sample XML Generated:

<?xml version="1.0" encoding="utf-8"?>
    <feed xml:base="https://test.com">
        <id>313213123</id>
        <entry>
            <id>https://test.com/_api/Web/GetUserById(161)</id>
            <category term="SP.User" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
            <link rel="edit" href="Web/GetUserById(161)"/>
            <title/>
            <author>
                <name/>
            </author>
            <content type="application/xml">
                <m:properties>
                    <d:LoginName>i:0e.t|test.com|user_1</d:LoginName>
                    <d:Title>user_1</d:Title>
                    <d:Email>[email protected]</d:Email>
                </m:properties>
            </content>
        </entry>
        <entry>
            <id>https://test.com/_api/Web/GetUserById(162)</id>
            <category term="SP.User" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
            <link rel="edit" href="Web/GetUserById(162)"/>
            <title/>
            <author>
                <name/>
            </author>
            <content type="application/xml">
                <m:properties>
                    <d:LoginName>i:0e.t|test.com|user_2</d:LoginName>
                    <d:Title>user_2</d:Title>
                    <d:Email>[email protected]</d:Email>
                </m:properties>
            </content>
        </entry>
    </feed> 

Appreciate if someone can guide me on the OData query for either of the above, as I can't seem to get it right.

Alternatively, also welcoming any other suggestions on how I can accomplish the above.

3

1 Answer 1

0

If you are trying to add filter query based on group name, use SharePoint REST API endpoint like:

/_api/web/currentuser/groups?$select=Title&$filter=Title eq 'Visitors 2'

Example:

Here is the complete HTML and JavaScript code to check the group membership of currently logged in user based on group name:

<script type="text/javascript" src="/sites/SPConnect/SiteAssets/jquery.min.js"></script>

<button type="button" onclick="checkUserGroupMembership('Flow Owners')"> Check User Group Membership </button>

<script type="text/javascript">
    function checkUserGroupMembership(groupName) {
        var groupsEndpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser/groups?$select=Title&$filter=Title eq '" + groupName + "'";

        $.ajax({
            url: groupsEndpoint,
            type: "GET",
            headers: {
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose"
            },
            success: function (data) {                
                //console.log(data);
                if(data.d.results.length > 0) {
                    alert('User is member of the group: ' + groupName)
                }
            },
            error: function (error) {
                console.log(error);
            }
        });
    }
</script>

Output:

enter image description here

Additional Reference: Check if a user is a member of SharePoint group JS REST API using async await

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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