Saturday, October 5, 2013

Delete All SharePoint List Items Using PowerShell 2010

How to delete all list items from SharePoint list?

Follow the steps to delete all list items from SharePoint List

1. Create new PowerShell (.ps1) file with below script using Notepad

$Url = "http://site-url:5000"
$ListName = "Sales 2013"
$Web = Get-SPWeb $Url
$List = $Web.lists[$ListName]

if($List -eq $null)
{    
  Write-Error "The List cannot be found";return
}

Write-Warning "Deleting all list items from $($ListName)"
$Items = $List.GetItems()
Write-Host "Total Items to be deleted : $($Items.count)"

if($Items.count -gt 0)
{
  $shell = new-object -comobject wscript.shell
  $result = $shell.popup("Total Item to be deleted : $($Items.count) `nDo   you want to continue?",0,"Alert",4+32)

if($result -eq "6")
{
foreach ($item in $Items)
{
  $itemId = $item.ID
  $List.GetItemById($itemId).Delete()
  Write-Host "Deleted list item with id $($itemId)"
}
}
}
$List.Update()
$Web.Dispose()


2. Replace values of parameters $Url and $ListName

3. Save file in server hard drive (Example: D:\PowerShell\Delete-List-Items.ps1)

4. Open "SharePoint 2010 Management Shell" with "Run as administrator"

5. Navigate to the folder where the script  file is stored
    (Example: cd D:\PowerShell)

6. Select file to be executed
    (Example: .\Delete-List-Items.ps1)

7. Press enter and enjoy!!

Monday, September 2, 2013

Server Error in ‘/’ Application. Runtime Error in SharePoint 2010

Whenever you will get this runtime error (Server Error in ‘/’ Application), you need do following changes in web.config file

1. Web Application web.config

Open C:\inetpub\wwwroot\wss\virtualdirectories\<port>” and modify the web.config file

a. In customErrors tag set mode="Off" to "On
b. In SafeMode tag set CallStack="true" to false
c. In Compilation tag Debug="false" to true

2.SharePoint web.config (Need to modify if above changes won't work)
Open SharePoint (2010) root directory or 14 hive – C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS and 
modify the web.config file

a. In customErrors tag set mode="Off" to "On
b. In SafeMode tag set CallStack="true" to false
c. In Compilation tag Debug="false" to true

Wednesday, July 3, 2013

SharePoint Web Id using PowerShell

Get SharePoint Web Id using PowerShell


1. Open SharePoint 2010 management Shell (PowerShell) with administrator privileges

2. Execute following command


$site =  Get-SPSite "http://yoursite"
$web = $site.AllWebs["Web Display Name"]
#For root web
#$web = $site.RootWeb
$web.ID

3. Above script will show unique id of the web

Thursday, June 20, 2013

Update DateTime Field In Event Receiver

Updating DateTime Field In Event Receiver sometimes give error, if we use:
properties.AfterProperties["ApprovedTimeStamp"] = DateTime.Now.
ToString(); ;
properties.AfterProperties["ApprovedTimeStamp"] = DateTime.Now.ToShortDateString();

Above statements give error as,
Invalid date/time value. A date/time field contains invalid data. Please check thevalue and try again.


This issue is because, SharePoint date-time field accept values in predefined format.
To overcome this issue, try:
properties.AfterProperties["DueDate"] =
DateTime.Now.ToString("yyyy-MM-ddThh:mm:ssZ");

Tuesday, June 18, 2013

JavaScript to Create Media player In SharePoint 2010

Steps to Create Media player In SharePoint 2010 using JavaScript:

1. Create new page in SharePoint site

2. Get <div> on newly created page

Example: <div id=”videoHolder”></div>

3. If page is inherited from master page, find “PlaceHolderAdditionalPageHead” and insert script tag with source as “_layouts/mediaplayer.js”

Example: <script type="text/javascript" src="_layouts/mediaplayer.js"></script>

4. Have one more script tag with following content:

<script type="text/javascript" >

_spBodyOnLoadFunctionNames.push("CreateMediaPlayer");

function CreateMediaPlayer()
{
    var videoUrl = “/Videos/MyVideo.wmv”; // video URL
    insertVideo(document.getElementById("videoHolder"),videoUrl);
}
function insertVideo(videoHolder,videoURL)
{
    mediaPlayer.createMediaPlayer(videoHolder,videoHolder.id, '400px','266px',
    {
         displayMode:'Inline',

         mediaTitle:’Help Video',
         mediaSource:videoURL,
         previewImageSource:'',
         autoPlay:true,
         loop:true,
         mediaFileExtensions:'wmv;wma;avi;mpg;mp3;',
         silverlightMediaExtensions:'wmv;wma;mp3;'
    });
}
</script>


5. Make sure the 
value of variable "videoUrl" is set correctly.

5. Save the page. That’s it!!!

6. Open the page and see the magic.

Thursday, June 13, 2013

SharePoint Modal Window Using JavaScript

Two ways to open SharePoint Modal Window using JavaScript:

1. Using the DialogOptions class.
var options = SP.UI.$create_DialogOptions();

options.title = "My Dialog Title";
options.width = 400;
options.height = 600;
options.url = "/_layouts/DialogPage.aspx";

SP.UI.ModalDialog.showModalDialog(options);

2. Using a generic object.
var options = {
    title: "My Dialog Title",
    width: 400,
    height: 600,
    url: "/_layouts/DialogPage.aspx" };

SP.UI.ModalDialog.showModalDialog(options);

Ref.: http://msdn.microsoft.com/en-us/library/ff410058(v=office.14).aspx

Wednesday, June 5, 2013

Hide Left Panel/Quick Launch From SharePoint 2010 Pages Using CSS

Hide Left Panel/Quick Launch From SharePoint 2010 Pages using CSS:
1.   Use Simple CSS classes to hide left panel
#s4-leftpanel
{ display:none; }
.s4-ca
{ margin-left:0px; }
2.   For single page level change, add Content Editor web part on the page and inject above script in HTML section of that web part.

For all pages in the site, use this script in master page.

Example:
<style type=”text/css”>
#s4-leftpanel
{ display:none; }
.s4-ca
{ margin-left:0px; }
</style>