It is not always necessary to have the default page of a site called default.aspx.
You can use a page called mypage.aspx and use this one as default.
But when you iterate through a site, how do you find out, which page is the default one. Therefore use a code like this to find out.
First you need a SPWeb object of your site-url (e.g. your site-url is http://www.yourwebapp.com/yoursite/yoursubsite/)
string result = string.Empty;
SPSecurity.RunWithElevatedPrivileges(delegate{
SPWeb web = SPContext.Current.Site.OpenWeb("yoursite/yoursubsite");
result = web.RootFolder.WelcomePage;
});
The result value will be "Pages/mypage.aspx".
Donnerstag, 14. April 2011
Dienstag, 29. März 2011
Variations Display Name vs. Label
Always good to know.
Display Name is the name shown in the structure tree and the name of the site.
The label is the name of the variation.
Display Name is the name shown in the structure tree and the name of the site.
The label is the name of the variation.
Montag, 28. März 2011
How to: c# code in powershell script
I was looking for a way to add C# code to a powershell script, because sometimes you are not able to run everything with powershell.
It works like this:
first reference to the assemblies your code uses:
$referencingassemblies = (
"Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" ,
"Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c",
"System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
)
Then insert the source and store it in a variable. Be aware to have all the assemblies you are using referenced in your variable above:
$mysource = @"
using Microsoft.SharePoint.Publishing.Administration;
using Microsoft.SharePoint;
using System;
using System.Data;
using System.Xml;
using Microsoft.SharePoint.Publishing;
namespace MyNamespace
{
public static class MyClass
{
public static void Do(SPWeb rootWeb)
{
Console.WriteLine(rootWeb.Url);
Console.WriteLine(rootWeb.Title);
}
}
}
"@
Then add the code to the memory and call your code:
#Here you just add the assemblies and the code to the memory
Add-Type -ReferencedAssemblies $referencingassemblies -TypeDefinition $mysource -Language CSharp
#For this sample I will need another assembly
[system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint")
#Load the site-object of your webapp
$site = New-Object Microsoft.SharePoint.SPSite("http://www.mydomain.com")
#Use the rootweb of your site
$root = $site.rootweb
#Overload to your C# method. To call the method use in brakets the namespace and class.
[MyNamespace.MyClass]::Do($root)
In this sample above you already see how to combine powershell code with C# code. If you change something in your C# code, then close the powershell window and re-open, so the code will be loaded/added to memory again. Otherwise you won't see the changes you made.
You can copy the code-segments above into one powershell script file and give it a try.
It works like this:
first reference to the assemblies your code uses:
$referencingassemblies = (
"Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" ,
"Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c",
"System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
"System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
)
Then insert the source and store it in a variable. Be aware to have all the assemblies you are using referenced in your variable above:
$mysource = @"
using Microsoft.SharePoint.Publishing.Administration;
using Microsoft.SharePoint;
using System;
using System.Data;
using System.Xml;
using Microsoft.SharePoint.Publishing;
namespace MyNamespace
{
public static class MyClass
{
public static void Do(SPWeb rootWeb)
{
Console.WriteLine(rootWeb.Url);
Console.WriteLine(rootWeb.Title);
}
}
}
"@
Then add the code to the memory and call your code:
#Here you just add the assemblies and the code to the memory
Add-Type -ReferencedAssemblies $referencingassemblies -TypeDefinition $mysource -Language CSharp
#For this sample I will need another assembly
[system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint")
#Load the site-object of your webapp
$site = New-Object Microsoft.SharePoint.SPSite("http://www.mydomain.com")
#Use the rootweb of your site
$root = $site.rootweb
#Overload to your C# method. To call the method use in brakets the namespace and class.
[MyNamespace.MyClass]::Do($root)
In this sample above you already see how to combine powershell code with C# code. If you change something in your C# code, then close the powershell window and re-open, so the code will be loaded/added to memory again. Otherwise you won't see the changes you made.
You can copy the code-segments above into one powershell script file and give it a try.
Donnerstag, 24. März 2011
Always needed information
I often can't recall, which Request.Url property returns what string. So I have published here now the most usefull ones:
Request.Url.AbsolutePath: /test/test34/testpage.aspx
Request.Url.Host: localhost
Request.Url.Query: ?test1=true&test2=false&test4=string
Segments: /
Segments: test/
Segments: test34/
Segments: testpage.aspx
Request.Url.AbsoluteUri: http://localhost:1234/test/test34/testpage.aspx?test1=true&test2=false&test4=string
Request.RawUrl: /test/test34/testpage.aspx?test1=true&test2=false&test4=string
Request.Url.ToString(): http://localhost:1234/test/test34/testpage.aspx?test1=true&test2=false&test4=string
Request.Url.AbsolutePath: /test/test34/testpage.aspx
Request.Url.Host: localhost
Request.Url.Query: ?test1=true&test2=false&test4=string
Segments: /
Segments: test/
Segments: test34/
Segments: testpage.aspx
Request.Url.AbsoluteUri: http://localhost:1234/test/test34/testpage.aspx?test1=true&test2=false&test4=string
Request.RawUrl: /test/test34/testpage.aspx?test1=true&test2=false&test4=string
Request.Url.ToString(): http://localhost:1234/test/test34/testpage.aspx?test1=true&test2=false&test4=string
Donnerstag, 3. März 2011
Powershell - site-defintion installation
I had the following problem. I created a powershell script to install a solution, that contains a site-defintion to a web-application. The issue I ran into was, that the template I am using to create the SPSite in this web-application was not recognized. That means everytime I tried to use this custom template, I received the error ""
On the web I found the cause for this error. The template is not recognized in this session. When you open parallel another powershell window and list the templates (get-spwebtemplates), then you see your template. But not in the host window.
To avoid this error, you have to start another powershell process out of the host window. There you can work with the template.
Be aware to add a sleep function, because the install of the solution with the site-definition needs to be terminated before being able to work with it.
You can copy the host-script into a file called hostscript.ps1 and the new window script into a file called window.ps1
---- your host script ------- (rename the %% variables with your parameters)
#This function installs the solution
function InstallSolution([string] $solutionname, [string] $url)
{
#Check if solution already exists
$solution = Get-SPSolution | where-object {$_.Name -eq $solutionname}
if ($solution -eq $null)
{
#If it is not added yet. Add it and then deploy it.
Add-SPSolution -LiteralPath "%your solution directory%\$solutionname"
Install-SPSolution –Identity $SolutionName –WebApplication "http://$url" –GACDeployment
}
$solution = Get-SPSolution | where-object {$_.Name -eq $solutionname}
#Run the sleeper. It will wait until the solution is finally deployed
do
{
Start-Sleep -Seconds 10
}
until ($solution.Deployed -eq $true)
}
#Enter your parameters here
$webapplicationname = %webapplication-name%
$webapplicationport = %webapplication-port%
$hostheader = %host-header%
$url = %url%
$iisdirectory = %iis-directory%
$apppool = %app-pool%
$apppoolaccount = %app-pool-account%
$databasename = %database-name%
$databaseserver = %database-server%
$sitecollectionadmin = %site-collection-admin%
#Create a new web-application with the above mentioned parameters
New-SPWebApplication -Name $webapplicationname -Port $webapplicationport -AllowAnonymousAccess:$true -HostHeader $hostheader -URL "http://$url" -Path $iisdirectory -ApplicationPool $apppool -ApplicationPoolAccount (Get-SPManagedAccount $apppoolaccount) -DatabaseName $databasename -DatabaseServer $databaseserver
#Call the function above and overload the needed parameters
InstallSolution "%my solution name%", $url
#Here is your template-name. If you don't know yours, then just install the solution and run get-spwebtemplate. It lists all templates. Yours is then supposed to be there as well with the correct # value.
$mydefinitiontemplate = "%My Template%#0"
#Here the new console powershell-window opens.
$newwindow = New-Object system.Diagnostics.ProcessStartInfo
$newwindow.FileName = "powershell.exe"
#I am overloading some variables just to show, how this works
$newwindow.Arguments = "-noexit %your install directory%\window.ps1 $url $mydefinitiontemplate $sitecollectionadmin"
$process = [system.Diagnostics.Process]::Start($newwindow)
#The process will wait, until the window will exit and close
$process.waitforexit()
#Finally just reset the iis
iisreset
---- end host script -------
---- your new window script ------- (rename the %% variables with your parameters)
#Here you catch the overloaded parameters and fill some variables with it
$url = $args[0]
$mytemplatename = $args[1]
$sitecollectionadmin = $args[2]
#Create the new site-collection
New-SPSite -URL "http://$url" -OwnerAlias $sitecollectionadmin -Name %your site name% -Template $mytemplatename
#Tell the host-window, that this window closed
$Host.SetShouldExit(0)
---- end new window script -------
Then save those files somewhere and open powershell and type:
[your path of the hostscript.ps1 file]\hostscript.ps1
This script now creates the web-application, installs the solution with the site-definition and as soon as the solution is deployed, it opens a new console window, to run the second script, that creates a new site-collection in the web-application with the site-definition template you deployed. The window closes afterwards and the host-window then does an iisreset.
It took me quite long to find out about this speciality with site-definition solution deployment and using the template afterwards when creating a site-collection.
On the web I found the cause for this error. The template is not recognized in this session. When you open parallel another powershell window and list the templates (get-spwebtemplates), then you see your template. But not in the host window.
To avoid this error, you have to start another powershell process out of the host window. There you can work with the template.
Be aware to add a sleep function, because the install of the solution with the site-definition needs to be terminated before being able to work with it.
You can copy the host-script into a file called hostscript.ps1 and the new window script into a file called window.ps1
---- your host script ------- (rename the %% variables with your parameters)
#This function installs the solution
function InstallSolution([string] $solutionname, [string] $url)
{
#Check if solution already exists
$solution = Get-SPSolution | where-object {$_.Name -eq $solutionname}
if ($solution -eq $null)
{
#If it is not added yet. Add it and then deploy it.
Add-SPSolution -LiteralPath "%your solution directory%\$solutionname"
Install-SPSolution –Identity $SolutionName –WebApplication "http://$url" –GACDeployment
}
$solution = Get-SPSolution | where-object {$_.Name -eq $solutionname}
#Run the sleeper. It will wait until the solution is finally deployed
do
{
Start-Sleep -Seconds 10
}
until ($solution.Deployed -eq $true)
}
#Enter your parameters here
$webapplicationname = %webapplication-name%
$webapplicationport = %webapplication-port%
$hostheader = %host-header%
$url = %url%
$iisdirectory = %iis-directory%
$apppool = %app-pool%
$apppoolaccount = %app-pool-account%
$databasename = %database-name%
$databaseserver = %database-server%
$sitecollectionadmin = %site-collection-admin%
#Create a new web-application with the above mentioned parameters
New-SPWebApplication -Name $webapplicationname -Port $webapplicationport -AllowAnonymousAccess:$true -HostHeader $hostheader -URL "http://$url" -Path $iisdirectory -ApplicationPool $apppool -ApplicationPoolAccount (Get-SPManagedAccount $apppoolaccount) -DatabaseName $databasename -DatabaseServer $databaseserver
#Call the function above and overload the needed parameters
InstallSolution "%my solution name%", $url
#Here is your template-name. If you don't know yours, then just install the solution and run get-spwebtemplate. It lists all templates. Yours is then supposed to be there as well with the correct # value.
$mydefinitiontemplate = "%My Template%#0"
#Here the new console powershell-window opens.
$newwindow = New-Object system.Diagnostics.ProcessStartInfo
$newwindow.FileName = "powershell.exe"
#I am overloading some variables just to show, how this works
$newwindow.Arguments = "-noexit %your install directory%\window.ps1 $url $mydefinitiontemplate $sitecollectionadmin"
$process = [system.Diagnostics.Process]::Start($newwindow)
#The process will wait, until the window will exit and close
$process.waitforexit()
#Finally just reset the iis
iisreset
---- end host script -------
---- your new window script ------- (rename the %% variables with your parameters)
#Here you catch the overloaded parameters and fill some variables with it
$url = $args[0]
$mytemplatename = $args[1]
$sitecollectionadmin = $args[2]
#Create the new site-collection
New-SPSite -URL "http://$url" -OwnerAlias $sitecollectionadmin -Name %your site name% -Template $mytemplatename
#Tell the host-window, that this window closed
$Host.SetShouldExit(0)
---- end new window script -------
Then save those files somewhere and open powershell and type:
[your path of the hostscript.ps1 file]\hostscript.ps1
This script now creates the web-application, installs the solution with the site-definition and as soon as the solution is deployed, it opens a new console window, to run the second script, that creates a new site-collection in the web-application with the site-definition template you deployed. The window closes afterwards and the host-window then does an iisreset.
It took me quite long to find out about this speciality with site-definition solution deployment and using the template afterwards when creating a site-collection.
Mittwoch, 23. Februar 2011
... Failed to create receiver object from assembly ... value cannot be null
I had this error, when I deployed a custom site-definition and then tried to create a new site-collection. The error was caused by a code I deployed some time before. I retracted those solutions and then deployed from scratch. It is important to clean out with old "not used" solutions. Also check in the Central-Administration, if all not used solutions are removed. After the new deployment, everything went smooth.
It could also be, that there is somewhere an assembly, that is called the same or does have similar namespaces.
It could also be, that there is somewhere an assembly, that is called the same or does have similar namespaces.
Dienstag, 22. Februar 2011
failed to instantiate file default.master not found.
This was a tough one. Trying to setup a site-definition and then received this error always, when trying to create a site.
My basic site-definition was extended with a module to initialize the masterpage, because I read on a website to do so.
It seems that the error is somewhere completely different. Got to the onet.xml and change the configuration-tag:
Usually most websites do show the onet.xml configuration tag as a relative path. But this change fixed my project.
My basic site-definition was extended with a module to initialize the masterpage, because I read on a website to do so.
It seems that the error is somewhere completely different. Got to the onet.xml and change the configuration-tag:
Usually most websites do show the onet.xml configuration tag as a relative path. But this change fixed my project.
Montag, 14. Februar 2011
the specified path, file name, or both are too long - Sharepoint 2010
Cut down your names. It might be, that your solution, files, etc. are too long. Just cut them down. Check the "project folder" property of your project. Is it not supposed to be very long. Try to keep it short.
the referenced file is not allowed on this page - Sharepoint 2010
It seems you are trying to register a user-control from the layouts folder. Put it into the controltemplates folder and it should work.
Donnerstag, 27. Januar 2011
Error Sharepoint 2010 "Could not load type" when loading a custom Page
Ich habe eine bestehende Sharepoint Seite durch eine eigene ersetzt und wollte dann in der Page Deklaration auf meine Klasse verweisen. Nach dem Deployment bekam ich oben genannten Fehler. Dieser tritt nicht auf, wenn man in der Page Deklaration im Inherit Attribut nicht nur auf die Klasse verweist, sondern auf die Assembly, den Token und Version.
Alt:
<%@ Page Language="C#" Inherits="[mein Namespace].[meine Klasse]" MasterPageFile="~/_layouts/dialog.master" %>
Neu:
<%@ Page Language="C#" Inherits="[mein Namespace].[meine Klasse], [Assemblyname], Version=1.0.0.0, Culture=neutral, PublicKeyToken=[Token ID]" MasterPageFile="~/_layouts/dialog.master" %>
I had replaced an existing Sharepoint page with a custom one and referenced in the page declaration to my class. After deployment I got the error mentioned above. This error won't raise, when you extend the inherit attribute with the assembly, token and version.
Old:
<%@ Page Language="C#" Inherits="[my namespace].[my class]" MasterPageFile="~/_layouts/dialog.master" %>
New:
<%@ Page Language="C#" Inherits="[my namespace].[my class], [assembly-name], Version=1.0.0.0, Culture=neutral, PublicKeyToken=[Token ID]" MasterPageFile="~/_layouts/dialog.master" %>
Alt:
<%@ Page Language="C#" Inherits="[mein Namespace].[meine Klasse]" MasterPageFile="~/_layouts/dialog.master" %>
Neu:
<%@ Page Language="C#" Inherits="[mein Namespace].[meine Klasse], [Assemblyname], Version=1.0.0.0, Culture=neutral, PublicKeyToken=[Token ID]" MasterPageFile="~/_layouts/dialog.master" %>
I had replaced an existing Sharepoint page with a custom one and referenced in the page declaration to my class. After deployment I got the error mentioned above. This error won't raise, when you extend the inherit attribute with the assembly, token and version.
Old:
<%@ Page Language="C#" Inherits="[my namespace].[my class]" MasterPageFile="~/_layouts/dialog.master" %>
New:
<%@ Page Language="C#" Inherits="[my namespace].[my class], [assembly-name], Version=1.0.0.0, Culture=neutral, PublicKeyToken=[Token ID]" MasterPageFile="~/_layouts/dialog.master" %>
Abonnieren
Posts (Atom)