Showing posts with label List. Show all posts
Showing posts with label List. Show all posts

Sunday, March 23, 2014

Sharepoint Powershell - Copy Column (Fields) to list

$web = Get-SPWeb http://siteURL           
$sourcelist = $web.Lists["SourceList"]
$destlist = $web.Lists["DetinationList"]

$columns=$sourcelist.fields



foreach($col in $columns)
{
 if( $destlist.fields.ContainsField($col))
               {                                      
           # Write-Host "found" $col
               }
               else
               {
              Write-Host "Field not found ="     $col, $col.Type
              Write-Host "Adding Field" 
              $destlist.fields.Add($col)
               }


}

Write-Host "Script Completed" 

Friday, May 3, 2013

Powershell - Display Event Receivers for a specific Sharepoint List


$web=Get-SPWeb "http://siteURL"
$list=$web.Lists["ListName"]
$list.EventReceivers
$web.Dispose()

Display Names of the Event receivers 

$web=Get-SPWeb "http://SIteURL"
$list=$web.Lists["ListName"]

$list.EventReceivers |ForEach-Object {Write-host $_.Type  $_.Name}
$web.Dispose()

Powershell - Create Posts list to Blog site in Sharepoint


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

$site=new-Object Microsoft.SharePoint.SPSite("http://siteurl/sites/test/")

$web=$site.OpenWeb()

$SPTemplate = $web.ListTemplates["Posts"]


$web.Lists.Add("Posts","Description",$SPTemplate)

Powershell - Delete list from Sharepoint site



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

$site=new-Object Microsoft.SharePoint.SPSite("http://siteurl/sites/test/")

$web=$site.OpenWeb()

$list=$web.Lists["ListName"]

$list.Delete()

Tuesday, April 10, 2012

How to delete Sharepoint list items permanently without being flushed to recycle bin


When we delete items using Processbatchdata, the deleted items are getting stored in the recycle bin. In order to avoid that we can set the recycle bin setting to “off” before deletion and to “on” after deletion as highlighted below.
Please note: Turning the recycle bin setting to OFF will delete the already stored item in recycle bin which is default behavior.



using (SPSite site = new SPSite("http://yoursiteURL"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    string batchFormat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                    "<ows:Batch OnError=\"Return\">{0}</ows:Batch>";

                    string processtext = @"<Method ID=""Del10"">
                                 <SetList Scope=""Request"">22BE7359-DD0D-4CD3-8920-584F3DED62E6</SetList>
                                 <SetVar Name=""Cmd"">Delete</SetVar>
                                 <SetVar Name=""ID"">10</SetVar>
                                 </Method>
                                ";

                    processtext = string.Format(batchFormat, processtext);
                    web.Site.WebApplication.RecycleBinEnabled = false;
                    string h = web.ProcessBatchData(processtext);
                    web.Site.WebApplication.RecycleBinEnabled = true;

                }

Saturday, January 28, 2012

Sharepoint - Field manipulation in sharepoint list programmatically

Add column to sharepoint list



using (SPSite siteCollection = new SPSite(url))
            {
                using (SPWeb web = siteCollection.OpenWeb())
                {
                     SPList list = web.Lists[“ListName”];
                      list.Fields.Add("fieldName", SPFieldType.Boolean, true);
                     list.Update();

   
                    }
                }


Delete column from list


using (SPSite siteCollection = new SPSite(url))
            {
                using (SPWeb web = siteCollection.OpenWeb())
                {
                     SPList list = web.Lists[“ListName”];
                      list.Fields[“fieldname”].Delete();
                     list.Update();

   
                    }
                }



Add column from site column list


using (SPSite siteCollection = new SPSite(url))
            {
                using (SPWeb web = siteCollection.OpenWeb())
                {
                     SPList list = web.Lists[“ListName”];
                      list.Fields.Add(siteCollection.RootWeb.Fields[fieldName]);
                                    list.Update();

   
                    }
                }