In a SharePoint Online (Office 365) environment we created a Sandboxed WebPart to put SPUsers in SPGroups. The SPGroup is created on rootlevel and has been given permissions on a subsite. This Subsite doesn't inherit permissions. We only use 1 SiteCollection.
We use the following code to add the User:
SPUser user = SPContext.Current.Site.RootWeb.EnsureUser(*loginname*);
Guid siteID = SPContext.Current.Site.ID;
Guid webID = SPContext.Current.Web.ID;
using (SPSite site = new SPSite(siteID))
{
using (SPWeb web = site.OpenWeb(webID))
{
try
{
web.AllowUnsafeUpdates = true;
//Get the group for this web
SPGroup oeGroup = web.Groups[*myGroupName*];
if (oeGroup == null) return false;
//Add the User to the OE Group
oeGroup.AddUser(user);
oeGroup.Update();
}
catch (Exception ex)
{
//Error Handling
}
finally
{
web.AllowUnsafeUpdates = false;
}
}
}
When I execute this code the ID of the user has changed afterwards. The ID (Used to lookup the User from the UserInfoList) of the user before this action is 24. After this action it's 4066. I can see that the user is in the correct Group. When I delete the user manually (using the UI) and put it back manually the ID is back to 24.
I need the correct ID (24) to make different lookups to the User in the entire site. When performing the action manually the ID is kept 24. Can anyone help me with this?