Upload file to SharePoint Online using PowerShell

Posted on

This script help you to upload your file or document to SharePoint library using PowerShell

  1. Download SharePoint Online client library from here , and install it. (If you already have it, please ignore this step)
  2. Create a folder named Temp on your C: drive, and put the Excel/CSV file in it
  3. Modify the parameters below and run the below script.


#Specify tenant admin and site URL
$User = "site.admin@tenantname.onmicrosoft.com"
$Password = "YourPassword"
$SiteURL = "https://tenantname.sharepoint.com/sites/site"
$Folder = "C:\Temp"
$DocLibName = "DocumentLibraryName"
#Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path "C:\Program Files (x86)\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files (x86)\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,(ConvertTo-SecureString $Password -AsPlainText -Force));
$Context.Credentials = $Creds
#Retrieve list
$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List)
$Context.ExecuteQuery()
#Upload file
Foreach ($File in (dir $Folder -File))
{
$FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $File
$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
}
#Upload completed

Let’s follow protocols: HTTP

Posted on Updated on

Some of us know a lot about what happen when we write URL in our browser and hit enter or when an app connect to a web service or API to get data or perform an operation, but let us discuss exactly what is going on between the browser or any client and the URL  or any server resource like HTML page, files, or web service … etc. As many knows HTTP sort for Hypertext Transfers Protocol so every thing happen is written as text on this dialog between the client and the server but what is exactly this message is and what is the server replay that what we will try to clarify.

First let us discuss the client which may be web browser, app, or even an web debugging tools like fiddler, Post Man, SOAP UI. which I think we need to talk about after HTTP.

Client Message or HTTP Request, in a happy world the request between client and server simply done by sending the request with a certain structure (we will discuss it in details) to the server and wait for the server response, but this request can go throw a long way by passing to proxy, gateway, and/or tunnel. In sort, A proxy is a forwarding agent, receiving requests for a URL in its absolute form, rewriting all or part of the message, and forwarding the reformatted request toward the server identified by the URL. A gateway is a receiving agent, acting as a layer above some other server(s) and, if necessary, translating the requests to the underlying server’s protocol. A tunnel acts as a relay point between two connections without changing the messages; tunnels are used when the communication needs to pass through an intermediary (such as a firewall) even when the intermediary cannot understand the contents of the messages.

 

 

HTTP Request message structure (Header and body):

1- Request Line contains [Request Method] + [Server Resource URL] + [HTTP Protocol version] + [Carriage return] + [Line feed]

ex: GET /pdf/book.pdf HTTP/1.1

2- Request header fields list, all header fields are optional except the host field. and the field structure is [Field Name] + [Colon] + [Field Value] + [Carriage return] + [Line feed]

ex: Host: tecgang.wordpress.com

3-Empty Line  [Carriage return] + [Line feed] in order to split between request header and request body

4-[Optional] Request body, which can be anything, binary file, text, JSON data or any data you want to send it to the server

HTTP Request methods are:

GET: The GET method requests a representation of the specified resource. for example to get an image or html file.

HEAD: The HEAD method asks for a response identical to that of a GET request, but without the response body. for example if you want to get a file but you want to know the file length first then you request a HEAD. (Useful for retrieving meta-information)

POST: Used to ask the server to accept the entity enclosed in the request as a new entity of the resource identified by the URL.

PUT: Used to ask the server to accept the entity enclosed in the request to store an entity of the resource identified by the URL. if it not exists its create new one or update existing one.

 DELETE: The DELETE method deletes the specified resource.

TRACE: The TRACE method echoes the received request so that a client can see what (if any) changes or additions have been made by intermediate servers.

OPTIONS: The OPTIONS method returns the HTTP methods that the server supports for the specified URL.

CONNECT: The client asks an HTTP Proxy server to tunnel the TCP connection to the desired destination. The server then proceeds to make the connection on behalf of the client. Once the connection has been established by the server, the Proxy server continues to proxy the TCP stream to and from the client.

PATCH: The PATCH method update partial resources.

 

 

HTTP Response message structure (Header and body):

1- Status Code and message [HTTP Version] + [Status Code] + [Message] + [Carriage return] + [Line feed]

ex: HTTP/1.1 200 OK

2- Response header fields, all header fields are optional and the field structure is [Field Name] + [Colon] + [Field Value] + [Carriage return] + [Line feed]

ex: Content-Type: application/pdf

3- Empty Line  [Carriage return] + [Line feed] in order to split between response header and response body

4-[Optional] Request body, which can be anything, binary file, text, JSON data or any data send by the server

 

 

Now you can see whats happen if you type in your browser an url like this one https://tecgang.wordpress.com/index.php, the dialog will be:

The Request:

GET /index.php HTTP/1.1
Host: tecgang.wordpress.com

The Response:

HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Content-Encoding: UTF-8
Content-Length: 138

<html>
<head>
<title>An Example Page</title>
</head>
<body>
Hello World, this is a very simple HTML document.
</body>
</html>

Retrieving Office 365 message center using PowerShell

Posted on

How to Retrieving Office 365 message center Information using PowerShell

Quick handy script to do that, and add the output to a SharePoint list, you can exclude the list part if you want
Prerequisite is to create the SharePoint list


Import-Module O365ServiceCommunications
# Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
# Specify tenant admin and site URL
$SiteUrl = "https://mycompany.sharepoint.com/sites/sitecollection"
$ListName = "MessageCenter"
$UserName = "admin@mycompany.onmicrosoft.com"
$SecurePassword = ConvertTo-SecureString "Pa$$word" -AsPlainText -Force
# Bind to site collection
$ClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$ClientContext.Credentials = $credentials
$ClientContext.ExecuteQuery()
# Get List
$List = $ClientContext.Web.Lists.GetByTitle($ListName)
$ClientContext.Load($List)
$ClientContext.ExecuteQuery()
Add-PSSnapin Microsoft.SharePoint.Powershell
$webUrl = "https://mycompany.sharepoint.com/sites/sitecollection"
$listName = "Message Center"
$web = Get-SPOWeb $webUrl
$list = $web.Lists[$listName]
$User = "admin@mycompany.onmicrosoft.com"
$Pass = "Pa$$word"
$cred = New-Object System.Management.Automation.PSCredential($User,(ConvertTo-SecureString $Pass -AsPlainText -Force));
#$cred = Get-Credential -Message "Office 365 Credentials" -UserName $username
# gather events from the Service Communications API
$MySession = New-SCSession -Credential $cred
$Events = Get-SCEvent -EventTypes Message -PastDays 100 -SCSession $MySession |
Select-Object Id, Title, StartTime, Service, EventType, Status, @{n='Message';e={$_.messages[0].messagetext}}
if ($Events)
{
$Tables = foreach ($Event in $Events)
{
$ListItemCreationInformation = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$newItem = $list.addItem($ListItemCreationInformation)
$newItem["Title"] = $Event.Id.ToString()
$newItem["MessageTitle"] = $Event.Title.ToString()
$newItem["Date"] = $Event.StartTime.ToString()
$newItem["Message"] = $Event.Message.ToString()
$newItem["Service"] = $Event.Service.ToString()
$newItem["EventType"] = $Event.EventType.ToString()
$newItem["Status"] = $Event.Status.ToString()
$newItem.Update()
$ClientContext.ExecuteQuery()
write-host "Item created: "
}
}
Write-Host "Completed"

Adding application insights to an existing Azure Application

Posted on

Adding application insights to an existing Azure Application

  1. Create a new application insights instance and associate to an existing app service
    1. Select New resource
    2. Look for “application insights”
    3. Give your new instance a name
      Give same name as app service to configure with application insights for ease of use

    4. Wait for the instance to be deployed successfully and browse to it
    5. Copy the instrumentation key (of iKey)
    6. Now browse to the related app service and open the application settings
    7. Add an application setting called ‘iKey’ and paste the instrumentation key copied previously
    8. Save and restart the service (although when you save app settings the app service restarts). Allow for a few minutes for data to be collected.
  2. Smoke tests
    1. Logs: It is also possible to check the logs for confirmation that the new settings were taken into account.
      1. In the Logging Framework logs: Ordering by descending EventDateTime, you should be able to find a couple of entries showing a restart of the application and that setting being applied
      2. Message should show:

        Note: The same should be added to the diagnostic logs should they be enabled
    2. Editor: Confirm the default value does not exist anymore
      1. Browse to the App Service Editor in your app service settings and click Go

      2. Select ApplicationInsights.config and scroll to the bottom. The default value for the instrumentation key is commented out.

  3. Other
    1. It is possible that if you created the application service with an associated application insights instance, it would give you the option to enable application insights.

      But this document assumes that application insights is added after the creation of the related application service

Export SharePoint Online Term Stores using PowerShell

Posted on Updated on

How to Export SharePoint Online Term Stores to CSV file, to allow you import it in any other environment ?

Unfortunately Microsoft Provides a simple out of the box way to import term stores using CSV template, but didn’t provide a way for exporting

Here below you will find a very simple Powershell Script that will allow you easy export your term stores so that you can clone it in any other tenant without hard efforts


Set-ExecutionPolicy -Scope CurrentUser Unrestricted
#Specify admin user and SharePoint site URL
#Update login
#Adding references to SharePoint client assemblies
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Taxonomy")
$GroupName = "Add Here the Group Name"
$TermSetName = "Add Here the TermSet Name you want to export"
$siteUrl = "https://tenant.sharepoint.com"
$UserName = "Put Your UserName Here"
$Pwd = Read-Host -Prompt "Enter your password" -AsSecureString
#Recursive function to get terms
function GetTerms([Microsoft.SharePoint.Client.Taxonomy.Term] $Term,[String]$ParentTerm,[int] $Level)
{
$Terms = $Term.Terms;
$Context.Load($Terms)
$Context.ExecuteQuery();
if($ParentTerm)
{
$ParentTerm = $ParentTerm + "," + $Term.Name;
}
else
{
$ParentTerm = $Term.Name;
}
Foreach ($SubTerm in $Terms)
{
$Level = $Level + 1;
#up to 7 terms levels are written
$NumofCommas = 7 - $Level;
$commas ="";
For ($i=0; $i -lt $NumofCommas; $i++)
{
$commas = $commas + ",";
}
$file.Writeline("," + "," + "," + "," + $Term.Description + "," + $ParentTerm + "," + $SubTerm.Name + $commas );
GetTerms -Term $SubTerm -ParentTerm $ParentTerm -Level $Level;
}
}
$mycreds = New-Object System.Management.Automation.PSCredential ($UserName, $Pwd)
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($mycreds.UserName, $mycreds.Password)
$Context.Credentials = $credentials
#$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
#$Credentials = New-Object System.Net.NetworkCredential($User,$Pwd);
#$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, $Pwd)
#$Context.Credentials = $Credentials
$MMS = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Context)
$Context.Load($MMS)
$Context.ExecuteQuery()
#Get Term Stores
$TermStores = $MMS.TermStores
$Context.Load($TermStores)
$Context.ExecuteQuery()
$TermStore = $TermStores[0]
$Context.Load($TermStore)
$Context.ExecuteQuery()
#Get Groups
$Group = $TermStore.Groups.GetByName($GroupName)
$Context.Load($Group)
$Context.ExecuteQuery()
#Bind to Term Set
$TermSet = $Group.TermSets.GetByName($TermSetName)
$Context.Load($TermSet)
$Context.ExecuteQuery()
#Create the file and add headings
$OutputFile = "D:\Out.txt"
$file = New-Object System.IO.StreamWriter($OutputFile)
$file.Writeline("Term Set Name,Term Set Description,LCID,Available for Tagging,Term Description,Level 1 Term,Level 2 Term,Level 3 Term,Level 4 Term,Level 5 Term,Level 6 Term,Level 7 Term");
$Terms = $TermSet.Terms
$Context.Load($Terms);
$Context.ExecuteQuery();
$lineNum = 1;
Foreach ($Term in $Terms)
{
if($lineNum -eq 1)
{
##output term properties on first line only
$file.Writeline($TermSet.Name + "," + $TermSet.Description + "," + $TermStore.DefaultLanguage + "," + $TermSet.IsAvailableForTagging + "," + $Term.Description + "," + $Term.Name + "," + "," + "," + "," + "," + "," );
}
else
{
$file.Writeline("," + "," + "," + "," + $Term.Description + "," + $Term.Name + "," + "," + "," + "," + "," + "," );
}
$lineNum = $lineNum + 1;
$TermTreeLevel = 1;
GetTerms -Term $Term -Level $TermTreeLevel -ParentTerm "";
}
$file.Flush();
$file.Close();

Get SharePoint Publishing Pages with PowerShell

Posted on Updated on

This code is to get all publishing pages that is created in the last 30 days.
It is compatible will all On-Prem SharePoint 2007, 2010, 2013 and 2016
Please open PowerShell-ISE and put the code in it. then click Run


$ErrorActionPreference = "SilentlyContinue"
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")
#Start-SPAssignment -Global
$newline = [Environment]::NewLine
$webAppURL="https://yoursharepointwebapplication.com"
$currentWebApplication = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($webAppURL)
$sites = $currentWebApplication.Sites
foreach($Site in $sites)
{
if ($Site.ServerRelativeUrl.Contains("market")){
#$Site = New-Object Microsoft.SharePoint.SPSite($SiteUrl)
$PubSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($Site)
$PubPageCount = 0
$newPubPageCount = 0
$ImageCount = 0
$DocCount = 0
foreach ($Web in $Site.AllWebs) {
$PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
$Pages = $PubWeb.GetPublishingPages($PubWeb)
foreach ($Page in $Pages)
{
if($Page.CreatedDate -GT (Get-Date).AddDays(-30))
{
$newPubPageCount += 1
}
}
$PageCount = $Pages.Count
$PubPageCount = $PubPageCount + $PageCount
#$Images = $PubWeb.ImagesLibrary.ItemCount
#$ImageCount = $ImageCount + $Images
#$Documents = $PubWeb.DocumentsLibrary.ItemCount
#$DocCount = $DocCount + $Documents
$Web.Dispose()
}
$OutputList += $Site.Url + "," + $Site.Allwebs.Count + "," + $PubPageCount + "," + $newPubPageCount +";"
$Site.Dispose()
#Stop-SPAssignment -Global
}
}
$OutputList

Convert Text to Excel using PowerShell

Posted on Updated on

In this post, we will convert text file to Excel using PowerShell.

Pre-requisite : you should have Microsoft Office Excel installed on your machine

1. This is a sample of the data to be converted, If you want to try it your self please Copy paste the below box to a file named sourceFile.txt

2. Now, open the PowerShell_ISE and paste the below script in it

#This script is to convert text files to formalized excel sheet.
#Please make sure txt file is applied in this format
#Header1, Header2, Header3, Header4
#X1, X2, X3, X4
#Y1, Y2, Y3, Y4
#Z1, Z2, Z3, Z4
#Constants
$SourcePath = "C:\temp\sourceFile.txt"
$DestinationPath = "C:\temp\destinationPath"
$deleimter= ","; #You can replace it by any other delimeter ';' or '/'#You can leave this part as it is
$SourceTxt = Get-Content $SourcePath
$xlsxFile = $DestinationPath + ".xlsx"if (Test-Path ($xlsxFile))
{
Remove-Item $xlsxFile
}
#$SourceTxt >> $txtFile
[threading.thread]::CurrentThread.CurrentCulture = 'en-US'
$xl=New-Object -ComObject "Excel.Application"
$wb=$xl.Workbooks.Add()
$ws=$wb.ActiveSheet
$xl.Visible=$True
$cells=$ws.Cells
$Content = Get-Content $SourcePath
$numOfRows = $Content.Length
$numOfColumns = $Content[0].split($deleimter).Count
for ($i=0; $i -lt $numOfRows ;$i++)
{
$rowData = $Content[$i].split($deleimter)
for ($j=0; $j -lt $numOfColumns; $j++)
{
$cellData = $rowData[$j]
$cells.item($i+1,$j+1) = $cellData
}
}
#Apply some Format for Excel header
$xl.Cells.EntireColumn.AutoFit()
$xl.Cells.EntireColumn.AutoFilter()
$xl.Rows.Item(1).Font.Bold = $True
$xl.Rows.Item(1).Interior.ColorIndex = 44
#Save Sheet
$wb.SaveAs($xlsxFile)
$wb.Close()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($wb)
$xl.Quit()
#Clean up
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
#Remove-Variable -Name xl
#[gc]::Collect()
#[gc]::WaitForPendingFinalizers()

3. Output should be something like this: