SharePoint / sp-dev-docs

SharePoint & Viva Connections Developer Documentation
https://docs.microsoft.com/en-us/sharepoint/dev/
Creative Commons Attribution 4.0 International
1.23k stars 992 forks source link

Sharepoint- unable to retrieve users from Sharepoint group #7381

Open jayapinjala opened 2 years ago

jayapinjala commented 2 years ago

What SharePoint development model, framework, SDK or API is this about? SharePoint C#

Developer environment Visual Studio Console App

Describe the bug / error I've used the code from the link https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ee538244(v=office.14) to retrieve the users from the Sharepoint Groups.

I am able to read the Group titles Successfully. However when I try to read the Users details of the group, I get an error

An unhandled exception of type 'Microsoft.SharePoint.Client.ServerUnauthorizedAccessException' occurred in Microsoft.SharePoint.Client.Runtime.dll Additional information: Access denied. You do not have permission to perform this action or access this resource.

I have full control on the Sharepoint Site.

Steps to Reproduce: The below piece of code executes successfully, the moment I uncomment the lines 45 to 52 I get the above mentioned Error.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.SharePoint.Client; using System.Net.Security; using System.Net;

namespace SPGroupusers { class RetrieveAllUsersInGroup { static void Main() { Console.Write("Enter UserName (domain/userid): "); string strUserName = Console.ReadLine(); Console.Write("Enter your password: "); string pass = getPassword(); Console.WriteLine();

        Console.Write("Enter Site URL: ");
        string strURL = Console.ReadLine();

        ClientContext ctx = new ClientContext(strURL);
        ctx.Credentials = new NetworkCredential(strUserName, pass);
        Web web = ctx.Web;

        ctx.Load(web, w => w.Title, w => w.SiteGroups);
        ctx.ExecuteQuery();

        GroupCollection collGroup = ctx.Web.SiteGroups;
        ctx.Load(collGroup);

        GroupCollection groups = web.SiteGroups;
        ctx.Load(groups, grps => grps.Include(group => group.Users));

        //ctx.ExecuteQuery();

        Console.WriteLine("Groups associated to the site: " + web.Title);
        Console.WriteLine("Groups Count: " + groups.Count.ToString());
        foreach (Group grp in groups)
        {
            Console.WriteLine(grp.Title);
            //UserCollection collUser = grp.Users;
            //ctx.Load(collUser);
            //ctx.ExecuteQuery();
            //foreach (User oUser in collUser)
            //{
            //    Console.WriteLine("Group ID: {0} Group Title: {1} User: {2} Login Name: {3}",
            //        grp.Id, grp.Title, oUser.Title, oUser.LoginName);
            //}
        }
        Console.Read();
    }

    private static string getPassword()
    {
        ConsoleKeyInfo key;
        string pass = "";
        do
        {
            key = Console.ReadKey(true);
            // Backspace Should Not Work    
            if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
            {
                pass += key.KeyChar;
                Console.Write("*");
            }
            else
            {
                if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
                {
                    pass = pass.Substring(0, (pass.Length - 1));
                    Console.Write("\b \b");
                }
            }
        }
        // Stops Receving Keys Once Enter is Pressed    
        while (key.Key != ConsoleKey.Enter);
        return pass;
    }

} 

}

Expected Behaviour: I would like to get the list of users.

ghost commented 2 years ago

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.