Category Archives: PowerShell

Additions to this Web site have been blocked – SharePoint 2010

Just got a very interesting error in my SharePoint 2010. Not exactly sure how it came to be … but anyways. I was making some PowerShell updates to the site … also restored the site using PowerShell … maybe this left the site locked down eventually.

First I could not edit or add anything to my SharePoint 2010 site. I tried to log-out and sign-in again. Than I restarted the server – this also didn’t help. Than I tried to change the site collection administrators and I got “Additions to this Web site have been blocked” error. Than I was closer to the solution … had to go actually to Central Administration > Application Management > Site Collections > Configure quotas and locks and change the “Lock status” for the site to Not locked.

additions-web-site-blocked-sharepoint-2010

PowerShell count number of characters in a string

I was just trying to count the number of characters in a string using PowerShell. It seems there is a very simple way to do it. It is a bit different if you are (me include) coming from a C language background like javascript, php or some other similar ones …

So the syntax is as follows:

1
2
3
4
$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
$measureObject = $string | Measure-Object -Character;
$count = $measureObject.Character;
Write-Host $count;

I have written the code above on the examples from Scripting Guy! Blog.

Get SharePoint list items and export them to XML using PowerShell

Last time I was explaining how to get sharepoint 2010 list items using powershell script. Now, lets make a step forward and export those items into an XML file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
cls
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$outputXmlFilePath="C:\Exports\yourfile.xml";
$webURL = "http://sharepointsite";
$listName = "listUwant2export";

$spWeb = Get-SPWeb $webURL;
$spList = $spWeb.Lists[$listName];
$spItems = $spList.GetItems();

[System.Xml.XmlTextWriter]$xml = New-Object 'System.Xml.XmlTextWriter' $outputXmlFilePath, ([Text.Encoding]::UTF8);
$xml.Formatting = "indented";
$xml.Indentation = 4;

$xml.WriteStartDocument();
$xml.WriteStartElement('root');

$spItems | ForEach-Object {

$xml.WriteStartElement('item');

$xml.WriteAttributeString("ID",$_['ID']);
$xml.WriteAttributeString("Title",$_['Title']);

$xml.WriteEndElement();
Write-Host $_['Title'];
}

$xml.WriteEndElement();

$xml.Flush();
$xml.Close();
$spWeb.Dispose();

The result will be an XML output with values as attributes.

Get items from SharePoint List using PowerShell script

For long time I wanted to dive a bit deeper into PowerShell. Hopefully I will be able to post few of the sharepoint powershell tricks here.
First one is about getting list items from a sharepoint list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cls
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
    Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$sourceWebURL = "http://sharepointsite"
$sourceListName = "mylist"

$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]
#$spSourceItems = $spSourceList.GetItems()
#$spSourceItems = $spSourceList.GetItemById("1")
$spSourceItems = $spSourceList.Items | where {$_['ID'] -eq 1}

$spSourceItems | ForEach-Object {
    Write-Host $_['ID']
    Write-Host $_['Title']
}

You can either get all the items with GetItems() method currently commented out or you can filter items by some parameters. Filter applied in the script above is also equal to the GetItemById(“1”) method.