Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

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

1 Answer 1

up vote 0 down vote accepted

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;
share|improve this answer
1  
Can you add the working sample code? –  Kit Menke Apr 13 '12 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. –  Paul Schaeflein Apr 13 '12 at 16:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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