1

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?

  • The only two possiblities are GetGroupName changes web or doesn't return "Marketing" – Per Jakobsen Jan 21 '18 at 13:34
  • I think it has something to do with SPFieldMultiLineText. That method reads value of department name from a list and that field is "Enhanced Rich Text" multiline. That method returns mlt.GetFieldValueAsText(item[ConfigurationsList.Value]) and even though it is string, for some reason it doesn't work. – Frank Martin Jan 21 '18 at 16:14
  • Instead of using a separate method that returns group name, I put all the code in line and still it has same problem. I have checked 100 of times and department name is same, even used .Trim().ToString() but same problem. Changed multiline field to single line of text and but issue. Looks like there is a bug in SharePoint. – Frank Martin Jan 21 '18 at 16:44
1

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"));
  • Your method got me thinking what the problem was. There were extra characters having code 8203 were coming in the string. I removed those characters and viola problem is solved. More about 8203 here: stackoverflow.com/questions/2973698/… – Frank Martin Jan 22 '18 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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