How to add mutiple custom Content Types to all Document Library(Existing or New) in MOSS programmatically

Hi SharePoint Experts,

Q:  How I can add(attach) mutiple custom Content Types to all Document Library(Existing or New) in MOSS programmatically(C#)? - so that there are multiple options for user to select which types of Documents they want to create under "New" menu. An Ideal solution would be using Feature Activated to attach these Content Types, and using Feature Deactiving to remove all these attached links - working sample codes?

Many thanks,
-Rick
LVL 1
RHADMINAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vikas413Commented:
Try the code given below, this is to add new content type in existing libraries.. and you can put this in feature activate event as well..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Collections;
namespace SPContentTypeAddInLibrary
{
  class Program
  {
    static void Main(string[] args)
    {
      using (SPSite site = new SPSite("http://localhost"))      
      {
        using (SPWeb web = site.OpenWeb())
        {
          SPListCollection lists = web.Lists;
          SPContentType targetContentType = web.ContentTypes["xyz"];
          bool targetTypeexists = false;
          foreach (SPList list in lists)          
          {
            if (list.BaseTemplate == SPListTemplateType.DocumentLibrary)
            {
              SPContentTypeCollection contenttypes = list.ContentTypes;
              targetTypeexists = false;
              foreach (SPContentType contenttype in contenttypes)
              {
                if (contenttype.Name == "xyz")
                {
                  targetTypeexists = true;
                  break;
                }
              }

              if (!targetTypeexists)              
              {
                list.ContentTypes.Add(targetContentType);
list.Update();
              }
            }
          }
        }
      }
    }

  }
}

to remove simply call List.ContentTypes.Remove("xyz");

enjoy

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RHADMINAuthor Commented:
Thanks, vikas413,
I'm actually looking for a more completed solution in code:
  1. create a custom Content Type(CT) named "GenericExcelFile"
  2. upload a custom document template(GenericExcel.xltx) for this CT
  3. Attach this CT to ALL document libraries without this CT attached
  4. 1-3 are done in Feature Activating
  5. undo(delete) these changes when Feature Deactivated
Thanks,
-Rick
RHADMINAuthor Commented:
Thanks vikas413 for your code, I'd finish the whole solution a bit later partly based on your contributed code.
Cheers,
-Rick
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SharePoint

From novice to tech pro — start learning today.