I'm encountering some problems getting "newly created" groups..
This is the code I'm using:
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SiteCollectionDestinazione))
{
using (SPWeb web2 = site.OpenWeb())
{
web2.AllowUnsafeUpdates = true;
site.AllowUnsafeUpdates = true;
try
{
web2.SiteGroups.Add(name, ownername, owner, description);
}
catch
{
throw new Exception("Already in use");
}
ritorno = web2.SiteGroups[name];
web2.AssociatedGroups.Add(ritorno);
web2.Update();
foreach (SPUser user in gruppoDaCopiare.Users)
{
ritorno.AddUser(user);
}
}
}
});
The things that makes me crazy is that another function very similar works:
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SiteCollection))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
site.AllowUnsafeUpdates = true;
try
{
web.SiteGroups.Add(name, ownername, owner, description);
}
catch
{
throw new Exception("Already in use");
}
ritorno = web.SiteGroups[name];
web.AssociatedGroups.Add(ritorno);
web.Update();
foreach (SPUser userin gruppoDaCopiare.Users)
{
ritorno.AddUser(user);
}
}
}
});
How could it be possible? What's wrong with this code? I also tried to Update the SPWeb after the "new user" created, but it doesn't work...
Any suggestion? Thank you very much!
EDIT: If I try to Add newly that group it tolds me that the name is already used but I cannot access it by SiteGroups["Name"]