Archive for May, 2012

May 30, 2012

Populate drop down with user created sharepoint fields

Assume that there is a situation where you have to expose only the user defined fields from a sharepoint list by filling up a dropdown. Usually when you tried to get fields collection list from a sharepoint list or a document library you will get list of columns roughly which will include around 40+ columns.

Following code snippet explains you how to populate a drop down list only with the fields that you have created.

Populatet the dropdown using a sharepoint list :

   using (SPSite site = new SPSite(siteUrl))
                {
                    using (SPWeb web = site.RootWeb)
                    {
                        SPList list = web.Lists["MainSpecificationList"];                        

                        SPList oSPList = web.Lists["MainSpecificationList"];
                        for (int i = 0; i < list.Fields.Count - 1; i++)
                        {
                            if (!list.Fields[i].Hidden && !list.Fields[i].ReadOnlyField &&
                            list.Fields[i].Type != SPFieldType.Attachments)
                            {
                                if (list.Fields[i].Title.ToString() != "Title" && list.Fields[i].Title.ToString() != "Content Type"
                                    && list.Fields[i].Title.ToString() != "SpecificationPDFURL" && list.Fields[i].Title.ToString() != "PDF View URL")
                                {
                                    ddlDropDown.Items.Add(list.Fields[i].Title.ToString());
                                }
                            }
                        }
}
}

Populate the dropdown using a sharepoint View:

   using (SPSite site = new SPSite(siteUrl))
                {
                    using (SPWeb web = site.RootWeb)
                    {
SPView view = list.Views["SearchView"];

                        SPListItemCollection itemCol = list.GetItems(view);                                                           

                        for (int Cnt = 0; Cnt <= list.Fields.Count - 1; Cnt++)
                        {
                            if (!list.Fields[Cnt].Hidden && !list.Fields[Cnt].ReadOnlyField &&
                            list.Fields[Cnt].Type != SPFieldType.Attachments)
                            {
                                if (list.Fields[Cnt].Title.ToString() != "Title" && list.Fields[Cnt].Title.ToString() != "Content Type"
                                    && list.Fields[Cnt].Title.ToString() != "SpecificationPDFURL" && list.Fields[Cnt].Title.ToString() != "PDF View URL")
                                {
                                    ddlDropDown.Items.Add(list.Fields[Cnt].Title.ToString());
                                }
                            }
                        }

                    }
                }
            
Advertisements
May 12, 2012

Shaerpoint Site Templates, Powershell commands to create subsites

The following list of template ids will be useful at the time when you are creating sub sites using powershell command lines.

GLOBAL#0 = Global template
STS#0 = Team Site
STS#1 = Blank Site
STS#2 = Document Workspace
MPS#0 = Basic Meeting Workspace
MPS#1 = Blank Meeting Workspace
MPS#2 = Decision Meeting Workspace
MPS#3 = Social Meeting Workspace
MPS#4 = Multipage Meeting Workspace
CENTRALADMIN#0 = Central Admin Site
WIKI#0 = Wiki Site
BLOG#0 = Blog
BDR#0 = Document Center
OFFILE#0 = Records Center
OFFILE#1 = Records Center
OSRV#0 = Shared Services Administration Site
SPS#0 = SharePoint Portal Server Site
SPSPERS#0 = SharePoint Portal Server Personal Space
SPSMSITE#0 = Personalization Site
SPSTOC#0 = Contents area Template
SPSTOPIC#0 = Topic area template
SPSNEWS#0 = News Site
CMSPUBLISHING#0 = Publishing Site
BLANKINTERNET#0 = Publishing Site
BLANKINTERNET#1 = Press Releases Site
BLANKINTERNET#2 = Publishing Site with Workflow
SPSNHOME#0 = News Site SPSSITES#0 = Site Directory
SPSCOMMU#0 = Community area template
SPSREPORTCENTER#0 = Report Center
SPSPORTAL#0 = Collaboration Portal
SRCHCEN#0 = Search Center with Tabs
PROFILES#0 = Profiles
BLANKINTERNETCONTAINER#0 = Publishing Portal
SPSMSITEHOST#0 = My Site Host
SRCHCENTERLITE#0 = Search Center
SRCHCENTERLITE#1 = Search Center
SPSBWEB#0 = SharePoint Portal Server BucketWeb Template

you can use the following power shell script to create sub sites within a site collection.

please put the following scripts in a file and name it as you wish (for ex: MyScript.ps1). Please make sure to change the site URL and the subsite names.

Add-PsSnapin Microsoft.SharePoint.PowerShell

## SharePoint DLL
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

#Creating Sub Sites in top site collection.
Write-Output " "
Write-Output "Creating Sub Sites"

$SiteCollectionURL = "http://localhost/"

$SiteCollectionTemplate = "BLANKINTERNET#2"

$SiteCollectionLanguage = 1025

$SubSites = @("ContactUs", "Membership", "Codes", "Specification", "HR")

$Names = @("ContactUs", "Membership", "Codes", "Specification", "HR")

for($i=0 ; $i -lt $SubSites.count ; $i++)
{
$SiteUrl = ""
$SiteUrl = $SiteCollectionURL + "/"
$SiteUrl = $SiteUrl += $SubSites[$i]
Write-Output " "
#Write-Output "Creating Site for " += $SubSites[$i]
Write-Output " "
New-SPWeb $SiteUrl -Template $SiteCollectionTemplate -Name $Names[$i]  -UseParentTopNav -Language $SiteCollectionLanguage
Write-Output " "
#Write-Output "Site Created for " += $SubSites[$i]
Write-Output " "
}

Remove-PsSnapin Microsoft.SharePoint.PowerShell

To automatically execute the above poweshell script you can create a small bat file as below:

cd /d %~dp0
powershell -noexit -file ".\MyScript.ps1" "%CD%"
pause