0

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"]

2
  • What problem are you having? Are you getting an error?
    – Kit Menke
    Apr 12, 2012 at 17:08
  • An exception on ritorno = web2.SiteGroups[name] that tolds me the group named "name" doesn't exist..
    – Ziba Leah
    Apr 13, 2012 at 6:57

1 Answer 1

0

Ok, i found out now what's the problem: I'm passing an SPMember instead of an SPUser as Owner, so it compiles, seems to work, but fails without raise any exception!

Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite site = new SPSite(SiteCollectionDestinazione))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    site.AllowUnsafeUpdates = true;
                    web.AllowUnsafeUpdates = true;

                    try
                    {
                    SPUser owner = web.EnsureUser(groupToCopy.Users.GetByID(groupToCopy.Owner.ID).LoginName);
                    web.SiteGroups.Add(groupToCopy.Name, owner, owner, groupToCopy.Description);
                    }
                    catch
                    {
                        throw new Exception("Group  " + groupToCopy.Name + " already exists");
                    }

                    web.AssociatedGroups.Add(web.SiteGroups[groupToCopy.Name]);
                    web.Update();

                    ritorno = web.SiteGroups[groupToCopy.Name];

                    foreach (SPUser utente in groupToCopy.Users)
                    {
                        SPUser utenteSito = web.EnsureUser(utente.LoginName);
                        ritorno.AddUser(utenteSito);
                    }
                }
            }
        });

        return ritorno;
2
  • 1
    Can you add the working sample code?
    – Kit Menke
    Apr 13, 2012 at 13:30
  • AllowUnsafe is a terrible idea in this scenario. See my answer here: sharepoint.stackexchange.com/questions/27990/… Also, RWEP is not necessary. That same answer presents a better alternative. If the current user cannot maintain groups, then why force it? You don't describe your scenario, so it is hard to give pertainant advice. Apr 13, 2012 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.