SharePoint Online: WebParts operation with PnP Powershell

First, you will have to connect to your SharePoint Online Site

Example: Connect to SPO

$siteurl = "https://yourspsite.sharepoint.com"  
Connect-SPOnline -Url $siteurl  
$ctx = Get-SPOContext  

==================== Accessing Web Parts on Page ====================

Retrieving a single web part

 
Get-SPOWebPart -ServerRelativePageUrl "Relateive Path to your page" -Identity "Web Part Identity"

Retrieving web part properties

 
$wp = Get-SPOWebPart -ServerRelativePageUrl "Relateive Path to your page" -Identity "Web Part Identity"

#Common Properties
$wp.Id
$wp.Title
$wp.ZoneId

#Other Properties
$wpProps = $wp.WebPart.Properties.FieldValues  
foreach($prop in $wpProps){  
    $prop ["XmlDefinition"]
}

Retrieving all web parts on a page

#this returns you the collection of web parts
$wps = Get-SPOWebPart -ServerRelativePageUrl "Relateive Path to your page"
#to loop through all the webparts
foreach($wp in $wps){  
     #Same as above to retrieve properties
     $wp.Id
     $wp.Title
     #etc...
}

==================== Adding Web Parts ====================

Add to Wiki Pages

Add-SPOWebPartToWikiPage -ServerRelativePageUrl "Relative Path to your page" `
                         -Path "Path to your .webpart/.dwp file" -Row 1 -Column 1 

Add to Publishing Pages

*You must check out the page before you can add any web parts

Add web part using webpart/DWP file

#check out the page for edit
Set-SPOFileCheckedOut -Url "Absolute Path to your page"

#adding webpart
Add-SPOWebPartToWebPartPage -ServerRelativePageUrl "Relative Path to your page" `
                            -Path "Path to your webpart/dwp file" `
                            -ZoneId "Header" -ZoneIndex 0

#check in the page
Set-SPOFileCheckedIn -Url "Absolute Path to your page" -CheckinType MajorCheckIn

Add web part using XML data

This options usually is useful when you are dealing with variable replacement

#check out the page for edit
Set-SPOFileCheckedOut -Url "Absolute Path to your page"

#retrieve xml from file
[string]$xml = (Get-Content "Source Path to your webpart file") `
               -replace "~variable~", "target value"

#adding webpart
Add-SPOWebPartToWebPartPage -ServerRelativePageUrl "Relative Path to your page" `
                            -xml $xml -ZoneId "Header" -ZoneIndex 0

#check in the page
Set-SPOFileCheckedIn -Url "Absolute Path to your page" -CheckinType MajorCheckIn

 

SharePoint Online: Page/List operation with PnP Powershell

First, you will have to connect to your SharePoint Online Site

Example: Connect to SPO

$siteurl = "https://yourspsite.sharepoint.com"  
Connect-SPOnline -Url $siteurl  
$ctx = Get-SPOContext  

Example: Retrieving page

$page = Get-SPOListItem -List "Pages" `
			-Query ("<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>" + $PageTitle + "</Value></Contains></Where></Query></View>") `
			-Web $Web
if($page -ne $null) {
	Write-Host "Page exists" -ForegroundColor Green
}
else {
	Write-Host "Page does not exist..." -ForegroundColor Red
}

Example: Retrieving all pages

$pages = Get-SPOListItem -List "Pages"  
foreach($page in $pages){  
    #Page Properties
    $page["Title"]  
    $page["FileRef"]
    #etc...
}  

Example: Adding a page

Add-SPOPublishingPage -PageName "testPage" ` 
                      -PageTemplateName "BlankWebPartPage" `
                      -Title "testPage" -Publish  

Example: How to update page properties

This can be also used to update the page layout of a publishing page


#Values type and reference from PnP site
#Single line of text: -Values @{"Title" = "Title New"}
#Multiple lines of text: -Values @{"MultiText" = "New text\n\nMore text"}
#Rich text: -Values @{"MultiText" = "New text"}
#Choice: -Values @{"Choice" = "Value 1"}
#Number: -Values @{"Number" = "10"}
#Currency: -Values @{"Number" = "10"}
#Currency: -Values @{"Currency" = "10"}
#Date and Time: -Values @{"DateAndTime" = "03/10/2015 14:16"}
#Lookup (id of lookup value): -Values @{"Lookup" = "2"}
#Yes/No: -Values @{"YesNo" = "No"}
#Person/Group (id of user/group in Site User Info List or email of the user, seperate multiple values with a comma): -Values @{"Person" = "user1@domain.com","21"}
#Hyperlink or Picture: -Values @{"Hyperlink" = "https://github.com/OfficeDev/, OfficePnp"}

#retrive the page
$PageTitle = "testPage"
$web = Get-SPOWeb -Identity "TestSubsite"
$page = Get-SPOListItem -List "Pages" `
			-Query ("<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>" + $PageTitle + "</Value></Contains></Where></Query></View>") `
			-Web $Web
#Update the page
Set-SPOListItem -List "Pages" -Web $web -Identity $page `
                -Values @{"Title"="Test Title"}

#Or alternatively if you know the page ID, e.g: 1
Set-SPOListItem -List "Pages" -Web $web ` 
                -Identity 1 -Values @{"Title"="Test Title"}

Example: Updating Publishing Page Layout

#retrieve page
$PageTitle = "testPage"
$web = Get-SPOWeb -Identity "TestSubsite"
$page = Get-SPOListItem -List "Pages" `
			-Query ("<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>" + $PageTitle + "</Value></Contains></Where></Query></View>") `
			-Web $Web

#check out the file for editing
Set-SPOFileCheckedOut -Web $Web -Url $page["FileRef"]

#updating properties
Set-SPOListItem -List "Pages" -Web $Web -Identity $page -Values @{"https://yourspsite.sharepoint.com/_catalogs/masterpage/BlankWebpartPage.aspx, Blank Web Part Page"}

#check in the update
Set-SPOFileCheckedIn -Web $Web -Url $page["FileRef"] -CheckinType MajorCheckIn -Comment "Checked in by script"   

Example: Deleting Page

$PageTitle = "testPage"
$web = Get-SPOWeb -Identity "TestSubsite"
$page = Get-SPOListItem -List "Pages" `
			-Query ("<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>" + $PageTitle + "</Value></Contains></Where></Query></View>") `
			-Web $Web
Remove-SPOListItem -List Pages -Identity $page -Web $web

SharePoint Online: Master Page Operation with PnP Powershell

First, you will have to connect to your SharePoint Online Site

Example: Connect to SPO

$siteurl = "https://yourspsite.sharepoint.com"  
Connect-SPOnline -Url $siteurl  
$ctx = Get-SPOContext  

Example: How to add master page

Add-SPOMasterPage -SourceFilePath "e.g: C:\temp\" -Title "master.aspx" `
                  -Description "Custom master page" `
                  -DestinationFolderHierarchy "Temp" -UiVersion 15

Example: Update master page settings

$MasterPageServerRelativeUrl = "/_catalogs/masterpage/Temp/master.aspx"
Set-SPOMasterPage -MasterPageServerRelativeUrl $MasterPageServerRelativeUrl `
                  -CustomMasterPageServerRelativeUrl $MasterPageServerRelativeUrl

Example: Update master page settings on subsites

$subWebs = Get-SPOSubWebs
$MasterPageServerRelativeUrl = "/_catalogs/masterpage/Temp/master.aspx"
foreach ($web in $subWebs) {
	Set-SPOMasterPage -MasterPageServerRelativeUrl $MasterPageServerRelativeUrl `
	                  -CustomMasterPageServerRelativeUrl $MasterPageServerRelativeUrl `
			  -Web $web.ServerRelativeUrl
}

Printing a Server Report without Preview only printing the first page

Problem
Printing a report without preview programmatically in C#, only printing the first page of the report if there are more than one pages.

1. From this example with local report without preview: http://msdn.microsoft.com/en-us/library/ms252091.aspx
2. We can print a report without preview, but it only prints the first page.
3. I’m using Server Report here as an example.

Solution

Instead of using this code to get the stream:

List<Stream> m_streams = new List<Stream>();
byte[] s = report.Render(“Image”, deviceInfo, out sMimeType, out sEncoding, out sExtension, out sStreamIds, out warnings);

Stream fs = new MemoryStream();
fs.Write(s, 0, s.Length);
m_streams.Add(fs);

We need to split it up into one stream per page:

List<Stream> m_streams = new List<Stream>();
//Split the stream per page
NameValueCollection urlAccessParameters = new NameValueCollection();
urlAccessParameters.Add(“rs:PersistStreams”, “True”);

//Get the first page
Stream s = _reportViewer.ServerReport.Render(“IMAGE”, deviceInfo, urlAccessParameters, out sMimeType, out sExtension);
m_streams.Add(s);
urlAccessParameters.Remove(“rs:PersistStreams”);
//Loop until all pages finish
urlAccessParameters.Add(“rs:GetNextStream”, “True”);
do
{
s = _reportViewer.ServerReport.Render(“IMAGE”, deviceInfo, urlAccessParameters, out sMimeType, out sExtension);
if (s.Length != 0) m_streams.Add(s);
}
while (s.Length > 0);

Then you will get the result with one stream per page.

Now you can replace the code to print reports with multiple pages with the Microsoft example.

Window Server 2008 IIS 7: 401 Unauthorized with NTLM after kerberos have been setup

In Window Server 2008, you don’t have the option to switch the provider like in Window Server 2008 r2.

So by default, if there are kerberos setup on the server, it will try to authenticate with kerberos first then NTLM even you have windows authentication enabled.

If you want to get this resolve, you will need to remove the Negotiate provider from the site you are hosting in order to get windows authentication working.

So you need to execute the following commands to remove the provider from IIS on a specific site:

The following code examples will enable Windows authentication and remove the Negotiate provider for a site named “SiteName”

appcmd.exe set config “SiteName” -section:system.webServer/security/authentication/windowsAuthentication /enabled:”True” /commit:apphost

appcmd.exe set config “SiteName” -section:system.webServer/security/authentication/windowsAuthentication /-“providers.[value=’Negotiate’]” /commit:apphost

* appcmd.exe is located at %systemroot%\system32\inetsrv\

Reference

http://www.iis.net/learn/get-started/getting-started-with-iis/getting-started-with-appcmdexe#HowToUse

http://www.iis.net/configreference/system.webserver/security/authentication/windowsauthentication/providers

Entity Framework: Dealing with views with no primary key

In entity framework there was a known issues or views with no primary will returns all the duplicated rows, because it couldn’t identity what’s different in the rows without a primary key.

There is a quick code fix we can apply to that,

Instead of using context.viewTest.toList(), we use the following:

In EF 5:

context.Database.SQLQuery<viewTest>(“Select * from viewTest”);

In EF 4:

context.ExecuteStoreQuery<viewTest>(“Select * from viewTest”);