0

I have this CSOM code:-

Microsoft.SharePoint.Client.Group group = context.Site.RootWeb.SiteGroups.GetByName(webname);                 
context.Load(group.Users);
context.ExecuteQuery();
int count = group.Users.Count();
FieldUserValue[] values = new FieldUserValue[group.Users.Count()];
      for (int i = 0; i < count; i++)
          {
            FieldUserValue fuv = new FieldUserValue();
            fuv.LookupId = group.Users[i].Id;//THIS WILL WORK FINE
            values[i] = fuv;
          }
User user = context.Web.CurrentUser;
if (group.Users.Any(a => a.Id == user.Id) && (currentItem["Team"] == null || currentItem["Team"].ToString().ToLower() != "cm"))//THIS WILL RAISE AN ERROR

now i am able to get the group.Users[i].Id without any issue, which on this code group.Users.Any(a => a.Id == user.Id) i got this exception :-

Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException
  HResult=0x80131509
  Message=The property or field 'Id' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

so why i can get the group.Users[i].Id without loading anything while i can not do the same for group.Users.Any(a => a.Id == user.Id)?? Thanks

1 Answer 1

1

First, you have already loaded group.users in below code:

context.Load(group.Users);
context.ExecuteQuery();

It has requested data from server and the related object has been initialized. So it's able to access group.Users[i].Id

The variable user in below code is another object.

User user = context.Web.CurrentUser;

It has not been initialized (ctx runtime has not send such a request). So we cannot access its ID.

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.