Gunady

HI All,
I have a requirement to send email to users from groups of certain sites in Sharepoint Services. Although it's very easy to retrieve all users by accessing SPGroups.Users, but if the user is an AD Group users, I am not sure what is the best way retrieve all users in the AD Group, that has accessed to WSS. Later, I found out that SPUserUtility has static method to do this, which is GetPrincipalsInSecurityGroup, but everytime I call this method, I get exception:

Object reference not set to an instance of an object. - at Microsoft.SharePoint.Utilities.SPUserUtility.GetPrincipalsInSecurityGroup(S-PWeb web, SecurityIdentifier sid, Int32 maxCount, Boolean& reachedMaxCount) at Microsoft.SharePoint.Utilities.SPUtility.GetPrincipalsInGroup(SPWeb web, String input, Int32 maxCount, Boolean& reachedMaxCount)

I already use curGroupUser.IsDomainGroup to check if it's AD Group, and pass the login name to SPUserUtility.GetPrincipalsInSecurityGroup.
Does anyone know why I get this exception I have checked all parameters has been passed, which I think it's already correct,
SPWeb -> Current Site instance
input -> spUser.LoginName
maxcount -> 100

Is there any other way to do this I don't have privilege/access rights to query directly to Active Directory.

Thanks


Re: SharePoint - Development and Programming Exception occured while calling SPUtility.GetPrincipalsInSecurityGroup

Unclepaul84

Try running your code wrapped in

Code Snippet

SPSecurity.RunWithElevatedPrivilages(delegate()

{

var users = Microsoft.SharePoint.Utilities.SPUserUtility.GetPrincipalsInSecurityGroup(......);

});






Re: SharePoint - Development and Programming Exception occured while calling SPUtility.GetPrincipalsInSecurityGroup

Gunady

Thanks for the answer.

I run the code in Windows Service with the service log on as Administrator/Site owner account. If the issue is related to access rights/privilege, I already run the code using Administrator account, so there shouldn't be any problem with the priveleges. Do you think if there is any other issue

The exception being thrown is like the code try to access null object: Object reference not set to an instance of an object. Simple checking in the code should prevent this to happen and report more meaningful error since we don't have any code to trace any object in the Sharepoint.





Re: SharePoint - Development and Programming Exception occured while calling SPUtility.GetPrincipalsInSecurityGroup

Curtis Ruppe (MicroStaff IT)

Try using SPUtility.GetAllAuthenticatedUsers method. It takes an SPWeb object, and returns a string value (Presumably an SPUserCollection object or something similar).



Re: SharePoint - Development and Programming Exception occured while calling SPUtility.GetPrincipalsInSecurityGroup

Gunady

Yeah, I have tried it before , but I am not sure what this static method is used for, when I call SPUtility.GetAllAuthenticatedMethod, it just returns one string, which is something like "NT Authority\System".



Re: SharePoint - Development and Programming Exception occured while calling SPUtility.GetPrincipalsInSecurityGroup

Curtis Ruppe (MicroStaff IT)

I was trying to find a method MS supported. Since they don't even mention that method is accessible, it tells me they don't support it. It may be leftover from implementation they had planned on, but never finished.

Have you tried looking into the UserInfo list directly





Re: SharePoint - Development and Programming Exception occured while calling SPUtility.GetPrincipalsInSecurityGroup

Gunady

Is there any other way to list all users of certain web/site I found there is one method of SPWeb, AllUsers, but it returns all the users that have visited the site, so if I happen to authorize the user to visit the site, and later I remove this user (unauthorize), but AllUsers still return this user, which becomes useless too.

I am still interested in using SPUtility.GetPrincipalsInSecurityGroup, but this method always throws exception when it's called: Object reference not set to an instance of an object. Is there anyway to check with Microsoft for this error Thanks.





Re: SharePoint - Development and Programming Exception occured while calling SPUtility.GetPrincipalsInSecurityGroup

Gunady

This is very weird and surprising. I try to call this method in Windows Service/Windows Application, the call to this method fail with exception: Object reference not set to an instance of object. But when I call this method through Web Service, it can be called succesfully without any error, and return the result as I have expected. The code is almost the same between windows and web application. Does anyone experience this

I suspect that the code inside this method try to access HttpContext or HttpUtility, which is supposed not to be, since the library of Sharepoint can be referenced from any kind of .NET application. Can anyone confirm this