Tuesday 27 December 2011

PowerShell - Script Comments

Within PowerShell their are two different types of comments:  Inline comments and Block comments.

Inline – #

Inline comments are denoted by a # symbol at the start of the line.  This comment only affects a single line.

Block – <#…#>

Block comments are opened using <# and closed using #> these comments can span multiple lines.

SharePoint 2010 - PowerShell Content Database Backup

“This post details the steps for backing up the content database of a SharePoint site through PowerShell.  Click here to view the steps for backing up a content database through Central Administration.”

PowerShell provides a means to backup data within SharePoint through the following command:

Backup-SPFarm -Directory [Path] -BackupMethod [Full|Differential] -Item [BackupItem] -Verbose -Percentage [Percentage]

Further information on each of the parameters in this command are outlined bellow.

Directory

The Directory parameters specifies the path where the backup will be generated.  This can either be a UNC path or a location on the server.   “You don’t need to enter the spbr* folder name as this will be auto generated”

BackupMethod

The backup method parameter details the type of backup that will be performed.  This can either be Full or Differential:

  • Full - backs up the selected content with all history
  • Differential - backs up all changes to the selected content since the last full backup
  • Item

    The item parameter specifies the component within the farm that will be backed up.  If the item has a unique name you can use just that, however, if it doesn’t then you will need to us the path to the item within the farm.

    In order to get the path to the item you can use the following command:

    Backup-SPFarm -ShowTree

    This will list the tree structure of items within the farm.  In my experience the path to a content database within the farm is in the following format:

    “Farm\Microsoft SharePoint Foundation Web Application\[Site Name]\[Content Database Name]”

    Verbos

    This parameter is optional.  The verbose parameter provides more information about the operation being run.

    Percentage

    This parameter is optional.  The percentage parameter can be used to report progress about the operation at defined percentage intervals.

    SharePoint 2010 - Central Administration Content Database Backup

    “This post details the steps for backing up the content database of a SharePoint site through Central Administration.  Click here to view the steps for backing up a content database through PowerShell.”

    Step 1

    Within Central Administration within the Backup and Restore section click on Perform a backup

    Perform a backup

    Step 2

    Next you need to select the component that you wish to backup.

    Select backup components

    Select the component you want to backup and click Next

    Step 3

    The next step in the backup process allows you to choose the type of the backup and its location.

    A backup can be either Full or Differential

    • Full - backs up the selected content with all history
    • Differential - backs up all changes to the selected content since the last full backup

    The backup file location can either be a UNC path or location on the server, however,  the process running the backup will need to access to write to this location.

    Backup Details

    Once the details of the backup have been entered click Start Backup

    STEP 4

    When the backup is running the following screen will be displayed indicating that backup is currently in progress.

    Backup - Running

    STEP 5

    When the backup has finished running the following screen will be displayed indication the status of the backup components.

    Backup - Status

    Friday 9 December 2011

    SharePoint 2010 - Timer job not updating after re-deployment

    When deploying a change to an existing timer job you need to restart the SharePoint Timer Job (owstimer.exe) service before the changes will be applied.

    This can be achieved through:

    net stop SPTimerV4
    net start SPTimerV4

     

    Note

    In my experience you need to restart the time job service on each web front end.

    Wednesday 7 December 2011

    SharePoint 2010 - PowerShell + IntelliSense = PowerGUI

    I recently came across a tool that makes authoring PowerShell scripts easier PowerGUI.

    PowerGUI and its in-built intellisense allows you to browse through the different PowerShell commands and unlock the power of PowerShell to automate administrative tasks and quickly change objects within the SharePoint API.

    You can download it from here

    Once you have downloaded and installed the program you need to load in the Microsoft.SharePoint.PowerShell library snapin.

    Step 1

    Within PowerGUI Click on File then PowerShell Libraries

    1 - Powershell Libraries

    STEP 2

    In the Snapins/Modules window Select the Microsoft.SharePoint.PowerShell library and Click OK

    2 - Snapins

    STEP 3

    You are now ready to start using the SharePoint PowerShell library.  Click New File

    3 - New File

    STEP 4

    Within the PowerGUI code editor when you type you should know have IntelliSense on the different SharePoint - PowerShell commands.

    4 - Intellisence

    STEP 5

    Once you have created your new script you can run it by clicking on the Start icon

    5 - Run Script

    STEP 6

    The output from your script will know be output in the console window.

    6 - Script Output

    Tuesday 6 December 2011

    SharePoint 2010 - ViewFormPagesLockdown

    Within an anonymous access SharePoint site you may wish to prevent users from navigating to the underlying lists e.g:

    • /_layouts/viewlsts.aspx
    • /Lists/[ListName]/AlItems.aspx

    This can be achieved by activating the ViewFormPagesLockdown feature.  This is a hidden feature, however, you can access it through PowerShell.

    Get-SPFeature | where { $_.DisplayName -eq "ViewFormPagesLockdown"}

    This will display:

    1 - ViewFormPagesLockdownThis tells us that the feature is scoped at the site collection level as well as the features ID.  The feature can then be activated using PowerShell similar to bellow:

    Enable-SPFeature -url http://sharepoint -identity 7c637b23-06c4-4724-9a9a-7c175762c5c4 -confirm:$false


    Anonymous users will now need to authenticate in order to view the underlying SharePoint lists and libraries.


    Note


    “As this feature is activated across a site collection this can lead to issues with anonymous users posting comments on blogs etc.. However the following blog details a method for overcoming this.”

    Monday 5 December 2011

    SharePoint 2010 - Enable Anonymous Access

    When creating an external facing SharePoint site you are probably going to have to configure anonymous access to allow users to view your site.

    You can achieve this by following these steps:

    Step 1

    Within central administration Click on Application Management then Manage web applications.

    1 - Application Management

    STEP 2

    Select the web application that you want to configure anonymous access for and Click on Authentication Providers.

    2 - Web Applications

    STEP 3

    Within the Authentication Providers window Click Default

    3 - Authentication Providers

    STEP 4

    Within the Edit Authentication window scroll down and Select the Enable anonymous access checkbox.  Then Click Save.

    4 - Edit Authentication

    STEP 5

    After editing the authentication Select the desired web application and Click Anonymous Policy

    5 - Web Applications

    STEP 6

    Within the Anonymous Access Restrictions window select None – no policy then Click Save

    6 - Anonymous Access Restrictions

    Step 7

    Within the web site you have configured anonymous authentication on navigate to Site Permissions

    You can achieve this through Site Actions – Site Permissions

    7 - Site Permissions

    STEP 8

    Within Site Permissions Click Anonymous Access.

    8 - Anonymous Access

    STEP 9

    Within the Anonymous Access window you can specify what items have anonymous access.  Select Entire Web site then click OK.

    9 - Anonymous Access (2)

    STEP 10

    Anonymous access should now be configured and your site permissions should display something similar to the following:

    10 - Site Permissions

    Note

    “After enabling anonymous access you may wish to prevent anonymous users from accessing the underlying SharePoint lists and libraries e.g:

    • /_layouts/viewlsts.aspx
    • /Lists/[ListName]/AlItems.aspx

    The can be achieved through the ViewFormPagesLockdown feature.  Click here for details on how to configure this.”

    Monday 21 November 2011

    SharePoint 2010 - Move Log Files

    By default the SharePoint Logs are located within the hive at:

    “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\LOGS”

    In order to mitigate the risk of running out of disk space on the C drive you may wish to change to location of the log files.

    This can be achieved through either Central Administration or PowerShell.

    Central Administration

    Within Central Administration click Monitoring then Configure diagnostic logging

    Configure Diagnostic Logging

    Within the diagnostic logging page,  scroll down to the Trace Log section and change the log file Path

    Diagnostic Logging

    PowerShell

    You can check the current location of the log files using:

    • Get-SPDiagnosticConfig

    Get-SPDiagnosticConfig

    You can set the location of the log files using:

    • Set-SPDiagnosticConfig –LogLocation “Path”

    Thursday 17 November 2011

    Visual Studio 2010 - Run with administrator privileges

     

    Visual Studio needs to be run with Administrator privileges in order to interact with SharePoint.  i.e to deploy features.

    If you aren’t running with administrator privileges you get the following error message:

    ElevatedPrivalages

    With the following message in the output console:

    “To interact with SharePoint projects in Visual Studio, your system user account must have administrator privileges.”

    In order to avoid this you can get Visual Studio to open up by default with administrator privileges:

    Step 1

    Right click on the Visual Studio shortcut icon and choose properties

    STEP 2

    Click on Advanced

    Visual Studio Properties

    STEP 3

    Select Run as administrator then click OK

    Advanced Properties

    Tuesday 8 November 2011

    InfoPath 2010 - Using UserProfileService.asmx

    “This blog details the steps for using the user profile service.  Click here for details on how to create a connection to retrieve information”

    The following steps outline how to pull through the logged in users “preferred name” from the User Profile Service web service.

    STEP 1 

    Right click a field within the fields data source and chose properties

    1

    STEP 2

    Within the text box properties window click the formulas button

    2

    STEP 3

    Within the insert formulas window click Insert Field or Group

    3

    STEP 4

    Within the Select a field or group window choose the GetUserProfileByName data source.  Expand the data fields and select Value then click Filter Data

    4

    STEP 5

    Within Filter data click Add

    5

    STEP 6

    With specify filter connection window click Select Field or group

    6

    STEP 7

    Within Select a field or group window select Name and click OK

    7

    STEP 8

    Within the specify filter conditions window enter a text filter: e.g “PreferredName”

    Then click OK

    8

    STEP 9

    The filter window should now show the following:

    Name = “PreferredName”

    Within the filter data window click OK

    9

    STEP 10

    Within the Select a field or group window click OK

    10

    STEP 11

    The insert formula window should now show:

    Value[Name="PreferredName"]

    Click OK

    11

    STEP 12

    The text box properties window should now show formula.  Then click OK

    12

    As well as the preferred name other information you can get from the user profile service are include in the table bellow:

    AccountName
    FirstName
    LastName
    WorkPhone
    Manager
    UserName
    WorkEmail

    InfoPath 2010 - UserProfileService.asmx

    “This blog details the steps for setting up a connection to this service.  See here for details on how to use the connection to retrieve information”

    User Profile Service

    The user profile service web service provides a means by which to read user profiles. 

    Within applications like InfoPath it can be used to provide a codeless method for accessing information about the user filling in the form.

    The User Profile Service web service can be accessed through the following URL:

    http://<server>/_vti_bin/UserProfileService.asmx

    The following steps can be used to add the link to the web service within InfoPath 2010

    Step 1

    Within the Ribbon click on the Data tab then Data Connections

    InfoPath Ribbon

    Step 2

    A list of all the current data connection available within InfoPath will be displayed.  Click on Add

    Add Connection

    Step 3

    The data connection wizard will now launch providing a means to create connections to submit and retrieve data.   Select Retrieve Data and click Next

    Connection Wizard Data Source Type

    STEP4

    The next window within the wizard allows you to specify which type of data source you wish to retrieve data from.  As the User Profile Service is a web service the select SOAP Web Service and click Next

    Retrieval Type

    STEP 5

    After selecting the data source type the next step is its location.  For the User Profile Service this is:

    http://<server>/_vti_bin/UserProfileService.asmx?WSDL

    Enter the location and click Next

    Web Service Url

    STEP 6

    After providing the link to the User Profile Service web service the wizard next displays all the operations that are available within it.   To retrieve information about the current logged user select GetUserProfileByName the click Next

    Web Service Method

    STEP 7

    As we want information about the current user the next screen within the wizard can be left with the default value.  Click Next

    Default Value

    STEP 8

    The next screen within the wizard can be left with the default values. Click Next

    Wizard Default

    STEP 9

    Within the final screen of the wizard provide a name for the new data connection.

    Check the box to Automatically retrieve data when form is opened

    Then click Finish

    Connnection Name

    A data connection for retrieving information from the User Profile Service should now have been created. 

    Tuesday 25 October 2011

    SharePoint 2010: The content type is in use

    When trying to delete a content type within SharePoint you can receive the following error:

    Capture

    Obviously it is telling you that you can’t delete the content type as it is being used somewhere within your site.  The question is where is the content type being used.

    When I have been faced with this I have used a couple of different methods to find out.

    SharePoint Manager

    SharePoint Manager is a tool that you can download here from CodePlex.  It allows you to explore various objects within your SharePoint farm,  including content types and their usages.

    Capture

    SharePoint Manager is a useful tool, however,  in some instances it will indicate that the content type has no current usages but you will still receive the above error message.2.

    SQL

    The following SQL can be used in instances where SharePoint Manager indicates that the content type is not being used.

    SELECT WebId, AW.FullUrl, ListId, AL.tp_Title
    FROM ContentTypeUsage As CTU
    INNER JOIN AllWebs As AW On CTU.WebId = AW.ID
    INNER JOIN AllLists As AL ON AL.tp_ID = CTU.ListId
    WHERE (sys.fn_varbintohexstr(ContentTypeId) LIKE '[ContentTypeID]%')


    You can replace [ContentTypeID] with the content type id within the URL when click on content type within SharePoint:


    image