Tuesday, 22 January 2013

PowerShell - Create/Update SharePoint 2010 data from excel file

PowerShell - Create/Update SharePoint 2010 data from excel file
#*=============================================================================
#* Purpose: To Create/Update list items with data extracted from excel file.
#*
#* Note: The Upload is done through OLEDB object, so there is no necessary for MS Office to be installed in the server.
#*=============================================================================

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$strFileName = Read-Host "Enter Excel Filename ::"
$strSheetName = Read-Host "Enter sheet name ::"
if ($strFileName -eq "") {
    throw "Please provide path to the Excel file"
    Exit
}
if (-not (Test-Path $strFileName)) {
    throw "File/Path does not exists - '$strFileName'"
    exit
}
if ($strSheetName -eq "") {
    throw "Sheet name cannot be null."
    Exit
}


##This fucntion is used to create new item..
Function fnCreateNewItem
{
param ([string] $webURL,[string] $listName,[string] $Title,[string] $Name,[string] $Address)
           
      #Get the SPWeb object and save it to a variable
      $web = Get-SPWeb $webURL

      #Get the SPList object to retrieve the "List"
      $list = $web.Lists[$listName]
     
      #Create a new item
      $newItem = $list.Items.Add()

      #Add properties to this list item
      $newItem["Title"] = $Title
      $newItem["Name"] =$Name
      $newItem["Address"] =$Address

      #Update the object so it gets saved to the list
      $newItem.Update()
}

##This fucntion is used to update existing item..
Function fnUpdateItem
{
param ([string] $webURL,[string] $listName,[string] $Title,[string] $Name,[string] $Address)
           
      #Get the SPWeb object and save it to a variable
      $web = Get-SPWeb $webURL

      #Get the SPList object to retrieve the "List"
      $list = $web.Lists[$listName]
     
      $spQuery = New-Object Microsoft.SharePoint.SPQuery
     
      #Query to filter data based on condition..
      $camlQuery = "<Where>
                              <Eq>
                                    <FieldRef Name='Title' />
                                    <Value Type='Text'>$Title</Value>
                              </Eq>
                        </Where>"
                       
      $spQuery.Query = $camlQuery  
      $spQuery.RowLimit =100 #Query Paging row limit..
      #Apply query and get all corresponding Item's...
      $spListItemCollection = $list.GetItems($spQuery)
      #Loop through every filtered items for data update...
      $spListItemCollection | ForEach-Object{
            $_["Name"] = $Name;
            $_["Address"] = $Address;
            $_.Update()
      }
}


$strSheetName = $strSheetName +'$'
$strProvider = "Provider=Microsoft.ACE.OLEDB.12.0"
$strDataSource = "Data Source = $strFileName"
$strExtend = "Extended Properties=Excel 8.0"
$strQuery = "Select * from [$strSheetName]"

$objConn = New-Object System.Data.OleDb.OleDbConnection("$strProvider;$strDataSource;$strExtend")
$sqlCommand = New-Object System.Data.OleDb.OleDbCommand($strQuery)
$sqlCommand.Connection = $objConn
$objConn.open()
$DataReader = $sqlCommand.ExecuteReader()
Write-Host "Processing...."
While($DataReader.read())
{    
      # Variables that we are going to use for list editing
      $webURL = http://<Site Url>      
      $listName = "<List Name>"
     
      #fnCreateNewItem $webURL $listName $DataReader[0].Tostring() $DataReader[1].Tostring() $DataReader[2].Tostring()
      fnUpdateItem $webURL $listName $DataReader[0].Tostring() $DataReader[1].Tostring() $DataReader[2].Tostring()
} 
Write-Host "Process Completed....!!!"
$dataReader.close()
$objConn.close()

Script Execution Steps:-
1) Pass PowerShell Script file name with entire path.
2) Pass Excel file name with entire path
3) Pass Excel Sheet name where the data exists.

Note: Web Url and List name has been hardcoded in above script, modify that to full fill your needs.

Configuring the Content Type Hub – User Manual

Configuring the Content Type Hub – User Manual
SharePoint 2010 now introduces a new feature called Content Type Hubs. Content Type Hub is a central location where you can manage and publish your content types – so now web applications can subscribe to this hub and pull down the published content types from the hub. Even receive updates on the published content types!

Procedure:-
1.       Create the web application and the root site which you want it to be the Content Type Hub. For eg:- http://<SiteURL>:<PortNo>/
2.       Now open the Central Administration, Click the Application Management. Under Service Applications, Click the Manage Service Applications

3.       Select the Managed Metadata Service and then Click properties as shown below

4.       You will get one Text box under the Content Type hub, Enter the URL of the Web App which you created in the first step [This Site Collection will acts as Content Type hub]
Also check the box below as shown in figure and Click OK.


5.       Now select the Managed Metadata Service Connection, and then click Properties as shown below

6.       Check the box of Consumes Content Types from Content Type gallery at [Your Web App which we configured in previous step]

This is for we explicitly tell to consume content types from the hub.

7.       Now, go to our content type hub site [which created in first step], Go to Site Actions and then Site Settings. Under Galleries click on Site Content Types, you will find default Content Type Available.
8.       Click on any Content Type where you can see Manage content type publishing option or else create a new Content Type by clicking Create option [ Recommended to create a new one to get clear idea]

9.       Click the Manage content type publishing and Check the Publish Radio button and say OK.


This will publish the content types

Timer Jobs:-
In order to receive the published content types immediately, you can go and run two timer jobs immediately:
1.       Now open the Central Administration, Click the Monitoring. Under Timer Jobs, Click the Review Job Definitions.

2.       Run the timer jobs listed below immediately to reflect the changes/implementation to all site collection.
a.       Content Type Hub
b.      Content Type Subscriber (of the web application which is going to receive the content types updates)




Published Content Types:-
After running the timer jobs, the content types should be published.

1.      Now open the Central Administration, Go to Site Settings | Site Collection Administration | Content Type Publishing.



  1. Check the Refresh all Published Content types on next update and Say OK


Note: As long as the Web Application use the same Metadata Service Application the content type hub is using, it can receive the content types from the hub.

To Verify:-
1.       Create a Site collection of same or different Web Application
2.       Go to the Site Actions -> Site Settings. Under the Galleries click the Site Content Types you will be finding the Site Content type which we created in above Site Collection.

The First Site Collection [Content Type Hub]:-




The different Site Collection under different Web App, able to see the same Content Type:-





------------------------------------------------------XXXXXX------------------------------------------------------

Microsoft Virtual Lab – User Manual

Microsoft Virtual Lab – User Manual

1.       Open web browser and navigate to URL given below.
2.       Click on “Launch Virtual Lab” button to start Virtual lab session.


Note: Active X controls should be enabled for the browser.

3.       Once after lab preparation a request will be prompted to use Clipboard on local computer, Check to enable the same and click on “Connect” button.

4.       Click on “Close Window” button, and then RDP for virtual computer will be listed.
          

5.       “Double Click” on computer name (“DC”) listed under “Remote Desktops” in left hand side of the screen. Once after this user will be successfully logged in to virtual machine.

A timer will be running on Top right corner of the screen, which shows the time available to close remote session.
        
Increase Remote Session Timing:-
1.       Click Menu -> Support->Troubleshooting/FAQ

2.       Click “And you would like to extend the duration……..” option, then click on “Click Here” which appears after above event. This action will increase your time by an hour. So user will be getting 3 hours continuously to practice SharePoint on virtual machine.
            
            

Use the credentials given below to play around SharePoint.
User Name: Administrator
Password: pass@word1
Note: You will loose all data once the session is closed, so please copy paste the project to your local machine in frequent intervals.  User credentials will remain unchanged for any instance of RDP session.