Dienstag, 27. November 2012

PublishingWeb unable to create page

I ran into a problem, where I wanted to create a page through PowerShell and then I received errors, when using the PublishingWeb object.

After a while I figured out, that you need to enable the web-scoped feature: SharePoint Server Publishing - 94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb

and then you might have to check if the web, does have a property-bag key "__PagesListId".
If not, create a key and save there the Guid of the document library, where you save the pages in. Default en-US install is "Pages"-library.
If you already do have the key, make sure, that the value corresponds to the Guid of the library, you save the pages in.

Dienstag, 22. Mai 2012

Delete items from a SPListItemCollection

Just a really dumb error I made when deleting items from a SPListItemCollection. This is my code:

SPListItemCollection listCollection = list.GetItems(spQuery);
for (int i = 0; i < listCollection.Count; i++)
{
listCollection.Delete(i); //listCollection.Delete(0)
}


What I did before was to go through the Count() method of the list-item-collection. This is wrong, because the amount of Count() changes during this operation. That means, not everything will be deleted. Use the code bellow. It will loop really through each item and will delete the dynamically changed first item of the list-collection.

SPListItemCollection listCollection = list.GetItems(spQuery);
int counter = listCollection.Count;
for (int i = 0; i < counter; i++)
{
listCollection.Delete(0);
}


That means you should be aware, that the index and the Count() method change, when performing a Delete() operation.

Freitag, 2. März 2012

SharePoint Webtemplate deployment error

When you want to deploy a webtemplate through Visual Studio and you get the following error:

"Error occurred in deployment step 'Add Solution': Exception from HRESULT: 0x8107026E"

Then you have just a naming problem. The WebTemplate Name-Attribute needs to have the same name as the EmptyElement folder-name.

Mittwoch, 29. Februar 2012

Error when deploying out of Visual Studio

I received an error when trying to deploy out of Visual Studio:
Error occurred in deployment step 'Activate Features': 0x80070005
The issue that caused this error was, that the user I was logged on my Windows, did not have administrative rights in the site-collection I wanted to deploy to.
I added my Windows user as an site-collection administrator and Voila, it worked.

Montag, 27. Februar 2012

Web templates HRESULT: 0x81070215

I wanted to create a web-template in Sharepoint 2010. This was supposed to substitude my site-definition. When trying to deploy, I received the error on "Add solution" with the error-code: HRESULT: 0x81070215.
I figured out, that I had the onet.xml in my folder, the elements.xml in my folder, but did not set the Deployment-type of the onet.xml correctly. Therefore go to the properties of the onet.xml and set it there. Check out the screenshot below:



Montag, 13. Februar 2012

How to get the IIS-directory of a webapplication with Powershell

There might be the need to get the IIS absolute path of your webapplication.
Here is the Powershell-script with which you can achieve that:

function GetIISPath ([string] $webApplicationName)
{
foreach($app in Get-SPWebApplication)
{
if($app.Name -eq $webApplicationName)
{
$IISSettings = $app.IisSettings[[Microsoft.SharePoint.Administration.SPUrlZone]::Default]
Write-Host $IISSettings.Path
}
}
}



This function writes the absolute path of the overloaded webapplication-name.


Mittwoch, 8. Februar 2012

value does not fall within the expected range -> Sharepoint list

I had the following code:
public static SPListItemCollection GetListItemCollection(string query)
{
SPListItemCollection listitemcollection = null;
SPList list = web.Lists["TestList"];
listitemcollection = list.GetItems(query);
return listitemcollection;
}


the issue was, that I did receive a SPListItemCollection object, but with no fileds in it, when debugging. When trying to access the field, the error displayed was:

I solved the problem in adding a SPQuery object to it. It seems, that the GetItems() method-overload, does not accept the query-string as a string overload. You need to pass it into the property of the SPQuery object and then overload with the object. This worked like a charm. Here the code:

public static SPListItemCollection GetListItemCollection(string query)
{
SPListItemCollection listitemcollection = null;
SPList list = web.Lists["TestList"];
SPQuery spquery = new SPQuery();
spquery.Query = query;
listitemcollection = list.GetItems(spquery);
return listitemcollection;
}


Freitag, 27. Januar 2012

performance counter registry hive consistency -> failed

I had the proeblem, when installing SQL server, that the performance counter registry hive consistency failed.
Error
Searched the internet quite a lot, but then found the correct solution.
http://geekswithblogs.net/robz/archive/2008/08/10/possible-performance-counter-corruption-or-performance-counters-are-just-disabled.aspx

You will have to go into the registry-editor and make there some changes.

1. Go to the HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib and check there in the subfolders, if your language is existing. There seems to be a strange calculation for the LCID.
Take the usual LCID, trim it to the last three digits and then replace the first with 0 and you have the folder-name.
E.g. German (Germany) = LCID 0407 ... trim to the last three digits = 407 ... then replace the first one with 0 = 007. That means, for Germany/German you will need to have a folder called 007 there.
E.g. English (US) = LCID 0409 = 409 = 009 folder-name
Find a list of LCIDs here:
http://msdn.microsoft.com/en-us/goglobal/bb964664

Registry folder structure

2. If your language folder is there, then go into it and you will need to have two keys in there: Counter and Help.
Just in case you don´t, then create them as multiline strings and copy the values from the 009 folder keys in there.
I also had one case, where I had to set the 'counter' and 'help' keys into every language folder.
Change value

3. Then go into both keys and check the last entry value. Usually both seem to incremental. In my case Counter had 2178 and Help 2179.
Check for values

4. Write down the values and go into the root folder and place those values as a decimal value into the keys Last Counter and Last Help.
Modify key Last Counter and Last Help

This change made it work in my case.

Donnerstag, 26. Januar 2012

Powershell script how to run a timer-job

If you want to run a specific timer-job, you can use this code (replace the value in the %%):

$myJob = Get-SPTimerJob "%Add the timer-job-name in here%"
$myJob.RunNow()

Powershell script how to add a field to a list

There might be the need for you to add a field to a list. Here is a script how to manage. Replace the values in %% with your values:

$isRequired = $%enter the boolean value in here%
$listName = "%enter your list-name%"
$siteUrl = "%enter the url of your site%"
$fieldName = "%enter the field-name%"
$fieldtype = "%enter the field-type%"

[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = new-object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.rootweb
$spList = $web.Lists[$listName]
if($spList -ne $null)
{
$spList.Fields.Add($fieldName,$fieldtype,$isRequired)
}
$web.Dispose()
$site.Dispose()

Powershell script how to create a list

You might want to create a list with Powershell. Here is some code you can use (Replace the values in % with your values):

[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$siteUrl = %your site-url%
$site = new-object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.rootweb
$templateType = $web.ListTemplates["Custom List %or add another list-template%"]
$web.Lists.Add("%enter the list-name you want to use%", "%enter the description%", $templateType)
$web.Dispose()
$site.Dispose()

Freitag, 20. Januar 2012

Powershell script how to change a property of list(s) or page(s)

Sometimes you might have to change a property of a list or page in a site-collection. Maybe the amount of items is so large, that handling this manually would take ages to do.
Here is a script, where I change all the list-titles to add the " 1" to it.

$sites = Get-SPSite
foreach($site in $sites)
{
$web = $site.RootWeb
foreach($list in $web.lists)
{
$title = $list.Title
$list.Title = "$title 1"
$list.Update()
}
}


This code changed now the title of each list in your web-pplications to it's origin name plus the string " 1". You can also check for a specific site-collection to make the changes at:
$sites = Get-SPSite
foreach($site in $sites)
{
if($site.Url -eq "%url of the site-collection you want to make the changes in%")
{
$web = $site.RootWeb
foreach($list in $web.lists)
{
$title = $list.Title
$list.Title = "$title 1"
$list.Update()
}
}
}

Powershell script how to add a webpart to a page

This script in no. 1 will add a webpart to a single page, no 2 will iterate through a site-collection and add the webpart to each sub-site/page. Please change the values in the %%- marked areas according to your settings/environment. Best practice would be to copy the code into PowerGui or any power-shell editor to see the code structured and the comments highlighted.

1. Adding a webpart to a single SPWeb
#load the assembly of your webpart
[void][reflection.assembly]::LoadWithPartialName("%your webpart assembly-name%")

#this code will do the publishing of the file, if publishing/versioning is disabled, you will not need this code
function PublbishSPFile([Microsoft.SharePoint.SPFile] $spfile)
{
if ($spfile.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
{
$spfile.CheckIn("%your comment%", [Microsoft.SharePoint.SPCheckInType]::MajorCheckin)
}
if ($page.ListItem.ParentList.EnableModeration)
{
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Pending)
{
$spfile.Approve("%your comment%")
}
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Draft)
{
$spfile.Publish("%your comment%")
}
}
}

#you have to get the web, where you want to add the webpart, you can also include everything into a function and call the function, when iterating, this is explained in no. 2
$site = Get-SPSite -Identity "%url of the site%"
$web = $site.RootWeb

#first you have to check out the file/page, if publishing/versioning is disabled, then you can skip this step
$file = $web.GetFile("/Pages/%your page%.aspx")
$file.CheckOut()
#initialize the webpart-manager and locate it to the page
$webpartmanager = $web.GetLimitedWebPartManager("%complete url%/Pages/%your page%.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

$webpart = new-object %add the object/class with namespace here, that corresponds to your webpart (e.g. Mynamespace.MyWebpartClass)%
$webpart.Title = "%title of the webpart%"
$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None;
#if your webpart does need some properties to be set, do it now
$webpartmanager.AddWebPart($webpart, "Zone1",0); #the code in the brakets adds the $webpart to the mentioned zone and sets the sorting of the webpart on the first place
$webpartmanager.Dispose();
#last but not least you have to check in the file/page, if publishing/versioning is disabled, then you can skip this step
PublbishSPFile($file)


2. Adding a webpart to multiple SPWebs
#load the assembly of your webpart
[void][reflection.assembly]::LoadWithPartialName("%your webpart assembly-name%")

#this code will do the publishing of the file, if publishing/versioning is disabled, you will not need this code
function PublbishSPFile([Microsoft.SharePoint.SPFile] $spfile)
{
if ($spfile.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
{
$spfile.CheckIn("%your comment%", [Microsoft.SharePoint.SPCheckInType]::MajorCheckin)
}
if ($page.ListItem.ParentList.EnableModeration)
{
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Pending)
{
$spfile.Approve("%your comment%")
}
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Draft)
{
$spfile.Publish("%your comment%")
}
}
}

#I used the code from no 1 and enclosed it into a function, that will be called in an iteration
function AddWebpart([string] $web)
{
#first you have to check out the file/page, if publishing/versioning is disabled, then you can skip this step
$file = $web.GetFile("/Pages/%your page%.aspx")
$file.CheckOut()
#initialize the webpart-manager and locate it to the page
$webpartmanager = $web.GetLimitedWebPartManager("%complete url%/Pages/%your page%.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

$webpart = new-object %add the object/class with namespace here, that corresponds to your webpart (e.g. Mynamespace.MyWebpartClass)%
$webpart.Title = "%title of the webpart%"
$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None;
#if your webpart does need some properties to be set, do it now
$webpartmanager.AddWebPart($webpart, "Zone1",0); #the code in the brakets adds the $webpart to the mentioned zone and sets the sorting of the webpart on the first place
$webpartmanager.Dispose();
#last but not least you have to check in the file/page, if publishing/versioning is disabled, then you can skip this step
PublbishSPFile($file)
}

$site = Get-SPSite -Identity "[url of the site]"
foreach($subsite in $site.AllWebs)
{
AddWebpart $subsite
}

Powershell script how to change the master-page

If you want to change your master-page in your whole site-collection with all sub-sites, then you can use this powershell-script to manage this quite easily (change the %%-values according to your settings or needs):

$site = Get-SPSite -Identity "%url of the site%"
foreach($subsite in $site.AllWebs)
{
$subsite.AllowUnsafeUpdates = "True"
$subsite.CustomMasterUrl = "/_catalogs/masterpage/%my master-page%.master"
$subsite.Update()
$subsite.Dispose()
}