Wednesday, May 16, 2012 2:09 PM
I have to separate solutions, call them solution A and solution B. Solution A contains lists and content types that and solution B contains content types as well. I need to programmatically attach some of the content types in solution B to some of the lists in solution A when these two solutions are deployed to a SharePoint site. Here is the code that I'm using:
SPList cmOrganization = web.Lists["CM Organization"]; SPContentType contentTypeOne = web.AvailableContentTypes["Content Type One"]; SPContentType contentTypeTwo = web.AvailableContentTypes["Content Type Two"]; SPContentType contentTypeThree = web.AvailableContentTypes["Content Type Three"]; cmOrganization.ContentTypesEnabled = true; cmOrganization.ContentTypes.Add(contentTypeOne); cmOrganization.ContentTypes.Add(contentTypeTwo); cmOrganization.ContentTypes.Add(contentTypeThree); cmCaseInformation.Update();
I'm not have luck with these content types being attached to the list. Could someone lend a second pair of eyes to see where this is going wrong?
Wednesday, May 16, 2012 2:29 PMAnswerer
can you try this code
SPList list = web.Lists["CM Organization]; SPContentType ctOne = web.ContentTypes[new SPContentTypeId("0x01010089e9684d7b344439a79a0ea5a2e6297e")]; list.ContentTypes.Add(ctOne); list.Update();
this one works for me (don"t forget to change the CT ID by your id)
Wednesday, May 16, 2012 2:52 PM
More detail please and more complete code sample, we can't see everything here
"I'm not have luck with these content types being attached to the list. Could someone lend a second pair of eyes to see where this is going wrong?"
There appears to be nothing wrong with your code aside from what you left out, can you tell us what error's you are getting?
Where the problem lies is probably in how you are deploying your content types and the order of execution of the features. E.g. If the code doing the above is firing before the content types in Solution B have been deployed, it's going to fail. Your feature in solution A should depend on the feature in Solution B deploying the content types. Deploy solution B first. Make the feature a feature dependency in solution A.
If all else fails, test in powershell (powershell is your friend) deploy both solutions then in powershell
$web = Get-SPWeb http://webwithlistsonit.com
$ctone = $web.ContentTypes["Content Type One"];
$ctone.Title #if this comes back with nothing then the content type isn't there!
#etc for other content types
$list = $web.Lists["CM Organization"]
Powershell is an awesome development tool, and great quick and easy testing platform. In case you are unaware, you can debug feature receivers in visual studio by attaching to Powershell.
Build and Gac your WSP Solution,
Then open a fresh copy of powershell,
Disable the feature
Disable-SPFeature SomeFeatureFolderNameHere -url Http://webwithlistsonit
Before clicking Y, attach visual studio to powershell with the debug menu
Then click Y
Enable-SPFeature SomeFeatureFolderNameHere -url http://webwithlistsonit
and Powershell will trip the breakpoints in your feature receiver.
This technique can literally save you weeks off a year in debugging time.
My Blog: http://www.thesug.org/Blogs/ryan_mann1/default.aspx Website: Under Construction
- Marked As Answer by Shaun Cline Wednesday, May 16, 2012 6:27 PM
Wednesday, May 16, 2012 5:58 PM
cmOrganization.ContentTypesEnabled = true;
Wednesday, May 16, 2012 6:29 PM
That was the issue, cmCaseInformation, I was updating the wrong list. After a while all the code sort of blends together doesn't it? Thanks man, take care!
Wednesday, May 16, 2012 6:31 PM
Found the issue, as Ryan pointed out I was updating the wrong the list. A second pair of eyes to look is always good!. You've helped out on several things recently, I should just send you my work and school projects :). Have a good day, Shaun.