0

I am trying to create an EventReceiver that works for more than one Content Type. When new Content Types are added in future I need the EventReceiver to still keep working.

My content types will always contain two works for the types of Content Type that I need. I need to execute some code when the ListItem is either of these two content types so I am getting the content type name.

I have the following code that checks for the existence of either of the content type names:

bool testStringA;
bool testStringB;
testStringA = sContentType.Contains("MyKeyWordOneInContentTypeName");
testStringB = sContentType.Contains("MyKeyWordTwoInContentTypeName");

if ((testStringA) || (testStringB))
{
    //DO SOMETHING BECAUSE THE LIST ITEM IS OF THE CORRECT CONTENT TYPE
}

Is there a better way to do this type of string comparison in code for this type of scenario?

3
  • 1
    Can you elaborate why you are looking for a 'better' way to do this? Commented Mar 10, 2015 at 14:56
  • I feel that my way of doing this is clunky and could be improved. I feel that there is a risk in using the Content Type name but I don't know another way to work with the Content Types in this scenario where I need to generalise the name of the content type so I can get my code to work with more than one and future Content Types that may be created. Commented Mar 10, 2015 at 15:01
  • 2
    I see. The alternative to checking the content type name is to check the content type ID. This would only be useful if content types created in the future inherited from a base content type that that you also created. For example, you create a content type, let's call it ContentTypeA, which has an ID of 0x0123456 (it will be longer than this in reality). Then, if future content types are created as children of this content type, you just need to do something like contentTypeID.StartsWith("0x0123456") in your event receiver code. Commented Mar 10, 2015 at 15:12

1 Answer 1

1

You can create a base content type for your types, then inherit from this content type, this way all of the child content types will have the same parent content type ID suffixed by their ID. So you could do only one if condition on the content type ID if it contains the parent content type, so something like the following Pseudocode:

if(sContentType.Id.ToString().Contains('parentcontenttypeId'){
//do something
}

So this way you will get all the content types inheriting from that content type without having to have static names anywhere. You would store the parentcontenttypeId in a constant variable in another class file to make your code structure better.

5
  • You should use sContentType.Id.Equals(parentCT.Id) || sContentType.Id.IsChildOf(parentCT.Id) to compare content type id's Commented Mar 10, 2015 at 15:15
  • But in this case I don't really want to compare child as mentioned, I want to see if the ID structure contains the parent or not. Which would be separated by 00 between parent and child, so you can do it using just string manipulations using Contains(). Right? Commented Mar 10, 2015 at 15:23
  • Yes you can, but that means your are handling the comparison yourself, instead of letting SharePoint methods do the work :) Commented Mar 10, 2015 at 15:45
  • This might work but how do I get the content type id from the list item? In my example sContentType is a string type variable. Commented Mar 10, 2015 at 16:55
  • You can get that from SPListItem.ContentType. Commented Mar 10, 2015 at 17:01

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.