Question

I am getting error Group cannot be found when I use following code:

string group = GetGroupName("1");  
SPUser user = web.SiteGroups[group].Users.GetByID(SPContext.Current.Web.CurrentUser.ID);

But strangely following code works fine:

string group = "Marketing";  
SPUser user = web.SiteGroups[group].Users.GetByID(SPContext.Current.Web.CurrentUser.ID);

The method GetGroupName is a static method that returns a string "Marketing". I don't know why it is not working in case1 but works in case2. I have checked hundred of times that the string returned by function is "Marketing" without any space or any other extra character.

Any idea why this is happening?

Était-ce utile?

La solution

We can add the method below to solve this issue.

static string GetRichTextValue(string value)
{
    if (null == value)
    {
        return string.Empty;
    }
    StringBuilder sb = new StringBuilder(value.Length);
    foreach (char c in value)
    {
        if (char.IsLetterOrDigit(c) || char.IsPunctuation(c))
        {
            sb.Append(c);
        }
    }
    return sb.ToString();
}

Then get group name using the following line of code.

string group = GetRichTextValue(GetGroupName("1"));
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top