CSC510235. Avoid SPObject.Name == <string> comparison.

Description

Depending on the case, SPObject.Name string based comparison is quite unsafe and might lead to the potential issues. In case of loop the result is performance degradation.

Follow properties are checked:
  1. SPPersistedObject.Name
  2. SPContentType.Name
  3. PageLayout.Name
  4. SPListItem.Name
  5. TaxonomyItem.Name (perform Group and Term checks)
  6. SPWeb.Name
  7. SPPrincipal.Name

string teststring = "some text";
SPWeb MyWeb = new SPSite("http://<Server>/sites/Team%20Site/default.aspx").OpenWeb();

TaxonomySession session = new TaxonomySession(MyWeb.Site);
TermStore termStore = session.TermStores["Company"];
Group group = termStore.Groups["Departments"];
TermSet termSet = group.TermSets["HR"];
Term term = termSet.Terms["HR specialist"];

if (group.Name != teststring) // <- wrong
{
   if (term.Name != teststring) // <- wrong
   {
      // Do stuf
   }
}

Resolution

  1. Use Object.ID instead or other key property.
  2. Git rid of string comparison in loop.

Links

No links are provided.

Last edited Jun 24, 2014 at 4:09 PM by dvd73, version 14

Comments

No comments yet.