Event receiver listadding and access the list

Hey,

 

I have this code in an event receiver. The event is listadded

 

string CTNameHDMDocument = "CTName";   

 

int HDMDocTemplateID = 10100;

 

if (properties.TemplateId == HDMDocTemplateID)

 {

 

    SPSite oSite = new SPSite(properties.WebUrl);

    SPWeb oWeb = oSite.OpenWeb();

    SPList oList = oWeb.Lists[properties.ListTitle];

 

    SPContentType oDocContentType = oWeb.AvailableContentTypes[CTNameHDMDocument];

    SPContentType oFolderContentType = oWeb.AvailableContentTypes[CTNameFolder];

    oList.ContentTypesEnabled = true;

    oList.ContentTypes.Add(oDocContentType);

    oList.ContentTypes.Add(oFolderContentType);

    oList.Update();

 }

The code works fine, but the user has to press F5 after the list generation.

What I has to change on the code when I want to hang the code an event listadding?

 

Thanks for your help

Regards

Stefan

March 24th, 2015 12:44pm

Hi Stefan,

According to your description, you might want to add content types to a list after it is created.

If put the code in the SPListEventReceiver.ListAdded Method which Handles the event that is raised after a list is added to a website, that means after the list been created, then the content types added into the list. As it is an asynchronous event, a possible scenario is that after the list view is rendered in browser, then the content types being added, in this case, an extra page refresh will be required on the client side to display the changes in content types.

Though there is another event ListAdding, as it Handles the event that occurs before a list is created on a website, it would not be able to meet your requirement either.

A workaround is that you can create a custom list definition with the content types needed and deploy to your site, then when users create a new list with this template, the content types will be there already.

About how to create a custom list definition:

http://www.codeproject.com/Articles/412429/SharePoint-Create-List-Definition-and-Instanc

Thanks         

Patrick Liang

Free Windows Admin Tool Kit Click here and download it now
March 25th, 2015 2:33am

Thanks Patrick for the help.

I need to use an event receiver. Because the problem is the solution must run in different SharePoint 2010 farms. On each farm the content type was created manually and has not a unique GUID.

Regards

Stefan


  • Edited by SK3110 Wednesday, March 25, 2015 2:24 PM
March 25th, 2015 2:24pm

Hi Stefan,

Can you provide more details about the whole scenario? Maybe there is already an existing solution for this requirement.

In my opinion, as it would not be an easy job to achieve in Event Receiver, I will still suggest you take the custom list definition way as all the content types can be arranged before list created, there will be no need to worry about the extra page refresh.

Thanks         

Patrick Liang
Free Windows Admin Tool Kit Click here and download it now
March 26th, 2015 11:17am

The solution must run in multiple SharePoint Farms.  In each farm there was created manually (over the guid) a document content type. The content types have the same name over all farms.

Now I have to create a custom document library which includes the custom document content type.

So I have created a custom document library definition with an event receiver. The event receiver fires by the event list adding and enabled content types in a list + adds the custom document type to the list.

In my test environment works it fine and fast. But in production it takes some time before the content type is added to the library. This is confusing for the user.

Thanks for your help

Stefan

March 26th, 2015 9:28pm

Hi Stefan,

Seems like a performance issue based on the difference between test environment and production environment.

You can trace the request using Fiddler to monitor what happen during the list creation, it will provide more information about how the whole process going and which part would be more time consuming. With this information, it would be easier to find out the root cause of this issue.

Thanks 

Patrick Liang

Free Windows Admin Tool Kit Click here and download it now
March 30th, 2015 10:46pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics