0

I am trying to set the content type programmatically using an itemadding eventreceiver so that when the user adds a document to a library the content type is pre-selected. I have two content types: Project Document and Document. I determine through code which type to use and set the column values. My problem is that I can't get the content type to switch to Project Document. Here is my code:

   SPContentTypeId ctId = properties.Web.ParentWeb.ContentTypes["Project Document"].Id;
    properties.AfterProperties["Content Type ID"] = ctId;
    properties.AfterProperties["Content Type"] = properties.Web.ParentWeb.ContentTypes[ctId].Name;

    properties.AfterProperties["ProjectID"] = "example id";
    properties.AfterProperties["Title"] = "random document";
    properties.AfterProperties["ProjName"] = "My Project";
    base.ItemAdding(properties);

When the properties form opens up the default content type (Document) is showing. When I switch to Project Document the values I added are there.

So my question is: How do I get it to change to the Project Document content type before the properties box opens?

0

Try setting "ContentType" and "ContentTypeId":

properties.AfterProperties["ContentTypeID"] = ctId;
properties.AfterProperties["ContentType"] = properties.Web.ParentWeb.ContentTypes[ctId].Name;
  • Paul - thanks for your reply. Unfortunately, no matter what I try (ContentTypeID and ContentType or Content Type ID and Content Type) I always end up with the default content type in the dropdown field. – Les Zatony Feb 23 '15 at 12:59
  • On second effort(and more careful capitalization), Paul's response was spot on. Thanks for your help. – Les Zatony Feb 23 '15 at 13:09
  • No problem. By the way, you can set just the ContentType. No need in setting ContentTypeID. – Paul Strupeikis Feb 23 '15 at 13:37

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.