Monthly Archives: June 2014

Getting Started: The Basics of Using PowerShell with SharePoint

I’m a SharePoint guy, and certainly no PowerShell expert. I know enough PowerShell to get things done in SharePoint from time to time. I can often go months without using PowerShell and talking to colleagues I don’t think I’m alone in the SharePoint community.

So if you are just starting out with using PowerShell to do something with SharePoint, or it’s been a while and you need a refresher on the basics then you’ve come to the right place.

Before we get started I’m writing this article based on SharePoint 2013 running on a Windows 2012 server.

The ground rules

1) Run your PowerShell scripts from the SharePoint server itself

It is possible to run PowerShell against a remote SharePoint server rather than on the server itself, but it requires some setup on the SharePoint server side and let’s face it, it’s not basic. If you need to do it you need to be searching for “Remote PowerShell in SharePoint”.

2) Use Windows PowerShell ISE (not SharePoint Management Shell)

PowerShell needs to be executed/run at a PowerShell Console or PowerShell Window – this window looks like a standard Command Line window and I don’t find it too inviting.

Instead of the SharePoint 2013 Management Shell, I use the Windows PowerShell ISE.

sharepoint-powershell-getting-started-cameron-dwyer-windows-powershell-ise

Why? It’s like comparing Visual Studio with Notepad. The ISE is an environment for developing PowerShell scripts that gives you nice syntax highlighting, debug with breakpoints, intellisense and more. It’s more like a development environment than a command line. I think you’d agree it looks slightly more advanced.

sharepoint-powershell-getting-started-cameron-dwyer-powershell-ise

3) Ensure the SharePoint PowerShell snapin is loaded

When using the “SharePoint 2013 Management Shell” (the ugly black one) it automatically loads a “snapin” which is basically a PowerShell extension that gives you a series of commands for working with SharePoint objects. When you use the Windows PowerShell ISE it has no idea of SharePoint, so you need to load the SharePoint snapin manually. The simplest way to do this is just to add the following code snippet to the start of all your scripts.

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
      Add-PSSnapin Microsoft.SharePoint.PowerShell
}

 

4) Save a script to file before you try to run it

Windows PowerShell ISE will have trouble running a script if you haven’t saved it to disk yet. PowerShell scripts are saved as files with a .ps1 extension.

 

5) Talk to SharePoint via the SharePoint PowerShell Cmdlets

Here’s a reference of all the SharePoint 2013 PowerShell Cmdlets you can use to work with SharePoint.

 

Your first script

Pre-flight checklist:

  • Start Windows PowerShell ISE
  • Add code snippet for loading the SharePoint snapin
  • Save the script as a .ps1 file

sharepoint-powershell-getting-started-cameron-dwyer-first-script

You can now start cutting and pasting examples from the internet and modifying them to work with your environment. The following 2 lines get a handle on the SharePoint website at the URL http://vs-server12 and then output the ID of the website.

To run the script and see if it work, click the Run button.

sharepoint-powershell-getting-started-cameron-dwyer-run-script

Any output from running your script is shown in the output window below the script editor.

sharepoint-powershell-getting-started-cameron-dwyer-output

You’re on your way, just remember you can only access the SharePoint server on which you are running your scripts. Accessing remote SharePoint servers is possible but you need to do special magic stuff to make that happen Smile

 

If you encounter an error along the lines of:

Get-PSSnapin : No Windows PowerShell snap-ins matching the pattern “Microsoft.SharePoint.PowerShell” were found.

You’ve ignored ground rule 4 and the pre-flight checklist by forgetting to save your script before running it (yes, I still do this too).

 

Advertisements

How to fix issue with opening Visual Studio solution from tfspreview.com by moving it to visualstudio.com

This problem arises if you were using Team Foundation Services (TFS) Online while it was still in preview. You will be accessing TFS on a URL such as: https://mycompany.tfspreview.com A recent change has been made to TFS Online that prevents you opening solutions using this URL. When you try to open an existing solution (that you have already synced down to your development machine ) you will see the following error message in Visual Studio:

image

Attempting to “Go Online” with the solution, or disconnect from TFS and reconnect using the same tfspreview.com address will result in a security credentials prompt that accepts your credentials but just sits there blank. BTW trying to login to the tfspreview.com URL in IE also results in this blank page behavior, in Chrome it gives a slightly better redirect error.

image

What Microsoft want you to use instead is the visualstudio.com address instead: https://mycompany.visualstudio.com.

This is simple if you just want to access TFS in a browser. Simply use the new address, but how do you get Visual Studio to start using the new URL to connect for a solution you already have on your dev machine?

Here’s the process that’s worked for me across Visual Studio 2012 and 2010.

Remove the existing TFS Connection from The Team Explorer

image

 

Now Add a TFS Connection using the visualstudio.com URL

image

 

Open the TFS Source Control Explorer now you are connected using the new visualstudio.com address

image

 

Select the solution folder for the solution you have synced to the local workspace on your development machine. Right click the solution folder and select the Get Specific Version… option from the context menu.

image

 

Ensure you check the option to “Overwrite all files even if the local version matches the specified version”. This will effectively pull all files for the solution from TFS again even if there has been no change made to the files. This has the side effect of fixing the binding of the files so that after the sync the files will now be bound to visualstudio.com instead of tfspreview.com.

image

 

Once the sync has completed you should see the following message:

image

 

Click Yes, and you will get a dialog letting you know of any offline changes.

image

 

That should be job done. Your Solution Explorer should now show the TFS icons indicating the source control state of all the files and you’ve now left the tfspreview.com URL behind.

Other links referencing this issue:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/4c887b08-60fe-40fb-8e25-81ab4c267003/cant-connect-to-tfspreview-in-vs11-but-can-via-ie?forum=TFService

 

How to bulk upload/copy a folder structure and files to SharePoint

So you’ve got a bunch of files nicely arranged in a folder structure sitting on your computer or a network drive that you want to copy up to a SharePoint library and preserve the directory structure. On the surface this is a daunting task as you can’t just give SharePoint your folder structure and tell it to do the work for you, so what are your options.

Option 1: Manually via SharePoint IU

Recreate the folder structure in SharePoint and upload the files via SharePoint UI in batches doing a multiple file upload to one destination folder at a time. Not much fun for anyone, but it can be done.

Option 2: Commercial Tool

Try a 3rd party tool. Yes they will probably cost you but will get the job done and probably with a few extra bell’s and whistles like applying metadata to SharePoint columns during the process. Some of the key companies to take a look at would be:

Option 3: Open Source Tools

There are some open source projects going around that claim to handle these types of bulk uploads, here’s a couple that look interesting:

http://spbulkdocumentimport.codeplex.com/

http://spfileupload.codeplex.com/

Option 4: Script it Yourself

PowerShell is an awesome way to get things done quickly in SharePoint, I’ve written these scripts in the past that show you how to create folders and how to upload documents to SharePoint. With a bit of extension and effort you could roll your own scripted solution.

https://camerondwyer.wordpress.com/2013/05/27/how-to-import-upload-an-entire-folder-of-files-to-sharepoint-using-powershell/

https://camerondwyer.wordpress.com/2013/11/06/sharepoint-powershell-how-to-create-sharepoint-library-folders-for-loadperformance-testing/

Option 5: Explorer View

The old ‘Open Library in Explorer View’ option in SharePoint may get you out of trouble if the directory structure and number of files is fairly small. Simply use the Explorer View to open the SharePoint library up in a Windows Explorer window and then copy/paste or drag/drop into the Window and the magic will start to happen

Option 6: OneDrive for Business

Now this is my preferred option for getting the job done, and the reason for writing this blog post! To achieve this one, browse to your library in SharePoint and click on the sync button to have the SharePoint library sync’d via OneDrive to your local machine.

bulk-upload-folder-structure-sharepoint-cameron-dwyer-onedrive-sync-button

After the sync has completed the SharePoint library (and it’s content including any folder structure) is now represented as a folder structure that you can easily get to through Windows Explorer.

bulk-upload-folder-structure-sharepoint-cameron-dwyer-onedrive-local-library

Now it’s just a matter of copy/paste or drag/drop your local folder structure (that you want to copy to SharePoint) into this folder in Windows Explorer. Here is my folder structure I want to copy up to SharePoint.

bulk-upload-folder-structure-sharepoint-cameron-dwyer-folders-to-upload

So I can just copy the “My Folder Structure” folder and paste it into the SharePoint sync library location. This is super quick because it’s just copying between 2 locations on your local hard drive.

bulk-upload-folder-structure-sharepoint-cameron-dwyer-onedrive-sync

Now the real goodness happens. OneDrive will sync the entire folder structure including any content files up to SharePoint in the background. You will notice that the folders and files you have just pasted have a tiny icon in the left corner. This will display a green tick when the item has been created in SharePoint. This is awesome, your computer is still free to use, there’s no lockup. If you need to shutdown before the sync is finished, it will just pick up from where it left off. OneDrive’s mission in life is to keep your local folder in sync with the SharePoint library, and it will work tirelessly in the background to do this.

Another powerful advantage this method has over many of the other options I provided earlier is that it will work with Office 365 which you just can’t reach some of those other methods.

Now my disclaimer before I get people throwing virtual rotten fruit at me. I’m not suggesting you use this method to move an entire network drive to SharePoint. Yes, I advocate you should take the opportunity to clean up the data on your network drives and not just move the problem to SharePoint. I also believe in the use of metadata to classify content rather than using folders. Having said that, there are valid instances when you do need to perform an upload of many files and preserve the folder structure in SharePoint and I think the OneDrive approach is a pretty cool tool to add to your arsenal.

%d bloggers like this: