Remove SharePoint List Views Using PowerShell


Ever have a need to delete list views using PowerShell?  Of course you do, it’s a great reason to use PowerShell!

Luckily for us, there is great access to the Object Model and we can readily access lists, items, and of course views.

I threw together a nifty little function that accepts three parameters, WebUrl, ListName and ViewName and will remove a single view from a SharePoint list.

The code is quite simple, basically we grab an SPWeb object using the WebUrl parameter – we then grab an SPList using the ListName parameter, and finally we grab an SPListView using the ViewName parameter.  Once we’ve drilled down to the specific SPListView, we can call the Delete() method using the ID of the view.

Here’s my function, enjoy!

function Remove-SPListView {
Param(
[string]$WebUrl,
[string]$ListName,
[string]$ViewName
)
Start-SPAssignment -Global
$SPWeb = Get-SPWeb $WebUrl
$List = $SPWeb.Lists[$ListName]
$View = $List.Views[$ViewName]
$List.Views.Delete($View.ID)
$List.Update()
$SPWeb.Update()
$SPWeb.Dispose()
Stop-SPAssignment -Global
}
Advertisements

Add Print Page Functionality Using JavaScript


This morning I received a request from my current client which I’ve gotten before, but not in SharePoint 2010.

They wanted the ability to print page and list view content easily, and without the left and top navigation elements – basically just the main content zone.

I knew there was a way as I had done this in 2007, but I had to do a quick search for the exact JavaScript.  I found a Codeplex project that included a Content Editor Web Part with JavaScript and this worked almost perfectly for my scenario.

This solution works great if you want to print a single page, but I wanted reusability and something that I could do once and have it work for every page in a site.

I ended up copying the script verbatim into a PrintScript.js file which I saved under /_layouts/1033, here is the code of the file:

<!-- Web Part/region to print -->
var WebPartElementID = "ctl00_MSO_ContentDiv";


//Function to print Web Part
function PrintWebPart()
{
 var bolWebPartFound = false;
 //var currentPage = document.location.href;
 if (document.getElementById != null)
 {
  //Create html to print in new window
  var PrintingHTML = '<HTML>\n<HEAD>\n';
  //Take data from Head Tag
  if (document.getElementsByTagName != null)
   {
   var HeadData= document.getElementsByTagName("HEAD");
   if (HeadData.length > 0)
    PrintingHTML += HeadData[0].innerHTML;
   }
  PrintingHTML += '\n</HEAD>\n<BODY>\n';
  var WebPartData = document.getElementById(WebPartElementID);
  if (WebPartData != null)
  {
   PrintingHTML += WebPartData.innerHTML;
   bolWebPartFound = true;
  }
  else
  {
   bolWebPartFound = false;
   alert ('Cannot Find Web Part');
  }
 }
 PrintingHTML += '\n</BODY>\n</HTML>';
 //Open new window to print
 if (bolWebPartFound)
 {
  var PrintingWindow = window.open("","PrintWebPart", "toolbar,width=800,height=600,scrollbars,resizable,menubar");
  PrintingWindow.document.open();
  PrintingWindow.document.write(PrintingHTML);
  // Open Print Window
  PrintingWindow.print();
 }
}

Once I had the JavaScript file saved in the layouts directory, I could simply add a ScriptLink tag in the <head> section of my master page:

<SharePoint:ScriptLink name="PrintScript.js" runat="server"/>

That’s great, now my master page has the ability to print any page; but where do I put the link?  At first I thought of adding a new DIV in the footer area of my page, but I didn’t want the Print link to be included in the printed version of the page.

A quick search on how to add an item to the Site Actions menu gave me EXACTLY what I wanted – and since this is an Intranet solution, every user that needs to print will have at least Contribute permissions and therefore will have the Site Actions menu available to them.

In order to display what I wanted in the Site Actions menu, I added the following code under the <CustomTemplate> section:

<!--Print item added to Site Actions -->
<SharePoint:MenuItemTemplate runat="server" id="MenuItem_PrintPage"
	Text="Print this page"
	Description="This will launch a new window to print this page."
	ImageUrl="/_layouts/images/Print.png"
	MenuGroupId="100"
	Sequence="110"
	ClientOnClickNavigateUrl="javascript:PrintWebPart();"
	/>
<!--End print item-->

And finally, here is what I’m left with:

And clicking the Print Page option gives me a nice, clean window with no navigational elements:

Create a Large SharePoint List Using PowerShell


SharePoint 2010 can support up to 30,000,000 items in a list with standard views, site hierarchies and metadata navigation. I’ve never seen a list come close to that, but I did set out to create such a list; mainly to see if it could be done. I didn’t go for 30 million items, I figured 1,000,000 was a good start…

Note: I did this on my VM, not in a client’s environment. 🙂

How did I have the time to create such a large list, you ask? Well, I didn’t. I used PowerShell to automate the whole thing.

PowerShell has become my new favorite tool, and it’s capabilities are truly endless.

The function I created uses standard PowerShell Cmdlets with some parameters to define your list. Here is an example of my script with the necessary parameters. This example would create 50,000 items in a list called MyBigList. Each list item would have a random choice (Red, blue or green) entered into the Category column defined in the list.

.\AddMultipleListItems.ps1 -ListName “MyBigList” -Amount 50000 -Choices “Red;Blue;Green” -ListItem “Added by PowerShell” -WebUrl http://intranet

I don’t know how many people will want to do this, but I can easily see adapting this script for other uses besides demo-ing large list functionality in SP2010.

Here’s the script:

#########################################################
## This script creates list items in a SharePoint list ##
#########################################################
####### Author: Ryan Dennis #############################
####### Twitter: @SharePointRyan ########################
####### Blog: www.iccblogs.com/blogs/rdennis ############
#########################################################

############# Variable Definitions ######################

## Number of list items to create ##
$amount = "1000"
## Start SP Assignment ##
$StartSpAssignment = Start-SPAssignment -Global
## Stop SP Assignment ##
$StopSpAssignment = Stop-SPAssignment -Global
## Define the categories ##
$choices = "Cat1;Cat2;Cat3;Cat4" -split ";"
## The List Name ##
$listName = "MyBigList"
## The Web Url ##
$webUrl = "http://towncenter/sites/rdennis"
## List Item Title ##
$listItem = "Added by PowerShell"

################## The Script ###########################
$StartSpAssignment
$mylist = (Get-SPWeb -identity $webUrl -AssignmentCollection $StartSpAssignment).Lists[$listName]
Write-Host "Creating $amount list items in $listName" -ForegroundColor Green
$i = 1
do 
{
    $newItem = $mylist.Items.Add()
    $newItem["Title"] = $listItem
    $newItem["Category"] = $choices | Get-Random
    $newItem.Update()
    $i++
}
while ($i -le $amount)

Write-Host "Finished!" -ForegroundColor Green
$StopSpAssignment