Scenario: A SharePoint document library or list containing various content types as seen in the screenshot below:

All Content Types

Suppose we want to show only two content types (HardwareItems and SanitaryItems) at the root of the site collection. This can be done by going into List Settings and clicking on Change new button order and default content type. Un-check the content types which are not needed as shown:

Change New Button Order

This is how same thing can be achieved through code:
using (SPSite site = new SPSite("http://aissp2013/sites/t1"))
{
    SPWeb web = site.RootWeb;
    SPList list = web.Lists["Hardware And Sanitary Products"];
    SPFolder folder = list.RootFolder;
    IList<SPContentType> uniqueContentTypeOrder = new List<SPContentType>();
    SPContentTypeCollection listContentTypes = list.ContentTypes;
    foreach (SPContentType ct in listContentTypes)
    {                   
        if (ct.Name == "SanitaryItems")
        {
            uniqueContentTypeOrder.Add(ct);
        }
        else if (ct.Name == "HardwareItems")
        {
            uniqueContentTypeOrder.Add(ct);
        }
    }

    if (uniqueContentTypeOrder.Count > 0)
    {
        folder.UniqueContentTypeOrder = uniqueContentTypeOrder;
    }
    list.Update();               
}
The main thing to note here is the folder is the root folder of the list. And this is the result.

Limited Content Types

Now suppose we want to limit folders of content type "HardwareItems" to show only content types "PowerTools" and "NutsAndBolts". This is how it can be done from UI. 
  • Create a folder of content type "HardwareItems" in the root folder of the list.
  • Then click on the folder created above and open the ECB menu. Click on "Change New Button Order".

Change New Button Order

  • Then select the two content types "PowerTools" and "NutsAndBolts" as shown:

PowerTools and NutsAndBolts

Finally once inside the folder of content type "HardwareItems", we will get to see only two content types:

PowerTools and NutsAndBolts

As can be seen this manual process needs to be followed every time a folder of content type "HardwareItems" is created. This can be automated through code. An "ItemAdded" event receiver can be added to the content type "HardwareItems" and then a code similar to what was written earlier can limit the folder types in "HardwareItems" folder to "PowerTools" and "NutsAndBolts".

The following example changes the layout of a page to ArticleLeft.aspx.
using (SPSite site = new SPSite("http://aissp2013"))
{
    SPWeb rootWeb = site.RootWeb;
    PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(rootWeb);
    PageLayout[] layouts = pubWeb.GetAvailablePageLayouts();
    PageLayout layout = null;
    var matches = from l in layouts
                  where l.Name.Equals("ArticleLeft.aspx", StringComparison.OrdinalIgnoreCase)
                  select l;
    if (matches.Count<PageLayout>() > 0)
    {
        layout = matches.First<PageLayout>();
    }                     
    if (null != layout)
    {
        SPFile file = rootWeb.GetFile("/Pages/MyClients.aspx");
        if (PublishingPage.IsPublishingPage(file.Item))
        {
            PublishingPage page = PublishingPage.GetPublishingPage(file.Item);
            if (page.Layout != layout)
            {
                file.CheckOut();
                page.Layout = layout;
                page.Update();
                file.CheckIn("Changed layout");
                file.Publish("Initial Publish");                           
            }
        }
    }
}

The example below uses SP.CamlQuery() to get items by setting set_folderServerRelativeUrl . 
<script type="text/javascript"> 
    var allItems;
    function GetFilesFromFolder(listTitle, folderServerRelativeUrl)
    {
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var list = web.get_lists().getByTitle(listTitle);
        // Use createAllItemsQuery to get items inside subfolders as well. Otherwise use new SP.CamlQuery() to get items from a single folder only
        var query = SP.CamlQuery.createAllItemsQuery();
        query.set_folderServerRelativeUrl(folderServerRelativeUrl);
        allItems = list.getItems(query);
        context.load(allItems, 'Include(File, FileSystemObjectType)');
        context.executeQueryAsync(Function.createDelegate(this, this.OnSuccess), Function.createDelegate(this, this.OnFailure));
    }
    function OnSuccess()
    {                                              
        var listItemEnumerator = allItems.getEnumerator();
        while(listItemEnumerator.moveNext())
        {
            var currentItem = listItemEnumerator.get_current();                                                                                                                     
            if(currentItem.get_fileSystemObjectType() == "0")
            {
                var file = currentItem.get_file();
                if(file != null)
                {
                    alert('File Name: ' + file.get_name() + '\n' + 'File Url: ' + file.get_serverRelativeUrl());                                                                                     
                }
            }
        }                                              
    }

    function OnFailure(sender, args) {
        alert("Failed. Message:" + args.get_message());
    }
</script>
The other way of getting the files inside folder is to use get_files() method of Folder
<script type="text/javascript">
    var files;
    function getFilesInFolder(folderServerRelativeUrl) {        
        var context = SP.ClientContext.get_current();
        var web = context.get_web();
        var folder = web.getFolderByServerRelativeUrl(folderServerRelativeUrl);
        files = folder.get_files();
        context.load(files);
        context.executeQueryAsync(Function.createDelegate(this, this.OnSuccess), Function.createDelegate(this, this.OnFailure));
    }
    function OnSuccess()
    {                                              
        var listItemEnumerator = files.getEnumerator();
            while (listItemEnumerator.moveNext()) {
                var fileUrl = listItemEnumerator.get_current().get_serverRelativeUrl();             
            }                                         
    }

    function OnFailure(sender, args) {
        alert("Failed. Message:" + args.get_message());
    }
</script>

My SharePoint CodePlex Projects

SharePoint 2010 Autocomplete Lookup Field

Export Version History Of SharePoint 2010 List Items to Microsoft Excel.