Thursday, August 30, 2012

Adding the contents of an entire folder into a Visual Studio 2010 project

If you'd like to bulk add the contents of an entire folder into the structure of a Visual Studio 2010 project (i.e. JQuery libraries are a perfect example), you'll soon discover that the Add -> Existing Item... route is not the way you want to go.  A quick solution for bulk adding a folder is to do the following actions:

  1. Open Windows Explorer and navigate to the folder you wish to copy
  2. Right-click on the folder and select Copy
  3. Open the applicable Visual Studio 2010 solution
  4. Select the project you wish to copy the folder to
  5. Navigate to the proper location in the project and right-click on the parent location of the folder
  6. Select Paste
The folder and all of it's sub-folders and files will now be copied into the specified location.  I know this seems like it would be obvious, but I seem to experience a moment of "how did I do that last time?" whenever it's been a while since I've created a solution that requires me to do this.  Good stuff!

Friday, August 24, 2012

Open a SharePoint 2010 List Item dialog via a hyperlink

One of our users was looking for a way to consolidate several links that, when clicked, would open the standard dialog to create a new list item for the associated List located on her SharePoint 2010 site in the same way that the "Add new item" link functions.

The solution we ended up using was introduced by one of my Avanade compatriots in the UK, Phil Childs, and his write up can be found at the following URL: Open list item modal dialog (lightbox) with a hyperlink in SharePoint 2010

The code that highlights one of the many links we added to our Content Editor web part to make this solution work is presented as follows:

<script type="text/javascript">
var optionsCABRequest = {
url: "http://SiteUrl/_layouts/listform.aspx?PageType=8&ListId={GuidOfOurSharePointList}&RootFolder=",
    title: "Create a new CAB Request",
    allowMaximize: true,
    showClose: true,
    width: 735,
    height: 580,
    dialogReturnValueCallback: silentCallback};
function openNewCABRequestForm() {SP.UI.ModalDialog.showModalDialog(optionsCABRequest);}
function silentCallback(dialogResult, returnValue) {
}
function refreshCallback(dialogResult, returnValue) {
  SP.UI.Notify.addNotification('Operation Successful!');
  SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
}
</script>
<br/>
<div>Click <a href="javascript:openNewCABRequestForm()">here</a> to create a new CAB Request</div>

Our thanks definitely goes to Phil for creating and sharing this solution. It worked perfectly for what our user was trying to accomplish.  Thanks, Phil!

Wednesday, August 22, 2012

Need to remove a problem web part?

If you've ever run into a situation in which a web part isn't working properly and is causing your web page to error out in such a way that it prevents you from being able to access/edit the page so that you can remove the offending web part (the InfoPath web part is notorious for this problem), here is a quick solution:
  1. Navigate to the web page on which the offending web part is installed
  2. Append the following text to the end of your URL in your browser's address window: "?contents=1"
  3. Hit Enter to refresh the browser
You will now be presented with the Web Part Page Maintenance view which will allow you to review a list of installed web parts and, potentially, close or remove the offending web part.  Here are the quick steps you can use to remove the web part:
  1. Review the list of web parts and place a check in the check box next to the offending web part
  2. Click the Delete link

Your web part will now be removed from the page so that you can attempt to perform a fresh install of the web part or, at the very least, allow your users to access the given page while you are given some time to figure out what went wrong with it.

Tuesday, August 14, 2012

How to create and run your first SharePoint Powershell script

As of late, I've been playing with Powershell quite a bit and thought I'd begin sharing some of the code samples that I've created and shared with our SharePoint Admin team.  For starters, I'd like to begin the journey by teaching those of you unfamiliar with Powershell how to create a simple script as well as execute it.  In this example, I'm going to create a script that performs several common actions that you're likely to run into when scripting in Powershell which are listed as follows:

  • Attach the Microsoft SharePoint assembly so that you can reference commonly used APIs
  • Accept input from the user
  • Write information to the console
  • Pause the window so that it doesn't close immediately after the script is run

When executed this script will do the following:

  • Prompt the user for the link to a site collection he/she would like to view a list of all sites contained under (i.e. http://SharePointWebApplication)
  • Write out the list of sites contained under the site collection to the console
  • Prompt the user to press any key to close the console

Creating a Powershell script:
  1. Open Notepad
  2. Copy and paste the following code into it:

  3. [System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
    $siteCollectionURL = Read-Host "Enter the URL of the Site Collection"
    $site = new-object Microsoft.SharePoint.SPSite($siteCollectionURL)
    
    foreach ($webs in $site.AllWebs)
    {
     Write-Host "URL:", $webs.Url
    }
    $site.Dispose()
    
    $Input = Read-Host "Please press any key to close the window"
    

  4. Click File -> Save As...
  5. Navigate to the directory you'd like to save your script under
  6. For File name:, enter a name for your file and add .PS1 to the end of it (i.e. ListAllSiteUnderSiteCollection.PS1)
  7. For Save as type:, select All Files
  8. Click Save

Executing the script:
  1. Right-click Start -> Open Windows Explorer
  2. Navigate to the directory where you just save your script
  3. Right-click on the script file and select Run with Powershell

Modifying the script:
  1. Right-click Start -> Open Windows Explorer
  2. Navigate to the directory where you just save your script
  3. Right-click on the script file and select Open With -> Notepad

I'll be certain to provide more scripts in the future, but at least wanted to start with the basics so that you'll know what do when you see them show up on my blog from time to time.

Thursday, August 2, 2012

Sample code for SharePoint ULS logging

The following code is a sample of how to write information to the ULS Log from your SharePoint-related code (in this case, I'd like to send an informational message from the code in my web part):

SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("My Web Part", TraceSeverity.Verbose, EventSeverity.Information), TraceSeverity.Verbose, "Message I'd like to log", null);

In the event you don't see the message showing up in the ULS Log, it could be possible that your SharePoint Admin is filtering out items with a low level TraceSeverity or EventSeverity such as those I'm using here (i.e. TraceSeverity.Verbose and EventSeverity.Information).  By the way, I'll be certain to cover how this is controlled in my next post.

Now, if you're interested in communicating error messages, there is a different method that you can use to write to both the Event Log and the ULS Log. The following code covers a simple way that you can communicate an error to both of those logs:

try
{
     ...
}
catch (Exception ex)
{
     SPDiagnosticsService.Local.WriteEvent(0, new SPDiagnosticsCategory("My Web Part ", TraceSeverity.Unexpected, EventSeverity.Error), EventSeverity.Error, ex.Message, ex.StackTrace);
}