I want to create new SPWebs with a Custom Template.
$web = New-SPWeb $Url
$web.ApplyWebTemplate("{GUID}#MyCustomTemplate")
My problem is now that the creation from one SPWeb with the Custom Template takes about 40 s on my VM. Is there a other and faster way to create SPWebs with a Custom Template?
Why not use -template parameter. For example,
New-SPWeb -url $url -template "{GUID}#MyCustomTemplate"
I did not have a custom template to use but this took 2 seconds on my system.
#LaPhi, you'll get an exception when your custom WebTemplate is not yet activated on your Site (Collection).
I, too, cannot get "New-SPWeb $Url -template "TemplateName" to provision with the Template. You'll likely need to wait until the site is provisioned.
My Solution:
First create the new Web:
$web = New-SPWeb $UrlFull
After this, apply the Web template
$web.ApplyWebTemplate("{GUID}#NameCustomTemplate")
The reason is, that the Custom Template is not global.
You have to use this syntax
New-SPWeb -url $url -template "MyCustomTemplateName#0"
You can find these values in Configuration tag of your webtemp_*.xml
<Configuration ID="0" Title="MyCustomTemplateName" ...
Related
I am using an Azure Function to create a new SharePoint site with PnP.
The Azure Function reads from a list in SharePoint that supplies all the Information needed to create a site.
This works Perfectly fine in Visual Studio Code, but not in the Azure Funktion.
The strange part is, that the after some time it just tells me the Funktion finished, eventhough some parts that syntactically cannot be skipped, got skipped.
Connect-PnPOnline -Url $siteUrl -Credentials $cred
Set-PnPListItem -List list -Identity $listitem.ID -Values #{"something" = "creating..."}
New-PnPSite -Type TeamSite -Title $listItem.Title -Alias $Listitem.Alias -Description $listItem.Description
Set-PnPListItem -List list -Identity $listitem.ID -Values #{"something" = "The URL"
This part of the Code works fine in in Visual Studio, but when it's run via Azure Functions, the list-field "something" first changes to "creating…", but then the Azure Function stops:
[Info] Function completed (Success, Id=x, Duration=6663ms)
I don't really get what is Happening, and why it stops, normally i would get an error message or something.
Do you guys know whats going on?
So, I just reinstalled all my modules to Azure Function + some new ones. After that my function worked as intended.
I have a Sharepoint Library, which I have a Powershell script dropping files into for processing. The Powershell script reaches out to Active Directory, and returns Group Membership information. The script then creates a folder for the group owner (if it doesn't exist) in my Library, using the group owners' name, and drops a .CSV of all the users contained in the specific group into that folder.
The need here, is to grant 'Read' permissions only to the owner of the group, which will be the name of the folder we are working in. Ideally the folder would be hidden, however I understand that there are limitations when working with Sharepoint.
For example:
John Doe, User: jdoe would be able to access Z:/jdoe/IT.csv but not
Z:/someuser/HR.csv
I have my Sharepoint Library mapped to Z:/ currently, to make my life easier for Powershell.
I executed get-command Module Microsoft.SharePoint.PowerShell | ft name and ran through the list of Sharepoint Commands.
I then stumbled across the Grant-SPObjectSecurity Cmdlet, which I assume is what I would want to use on the Powershell side to, when the folder is being created, apply Sharepoint permissions only to the user for which the folder is being created for.
The process from start to finish is: Powershell Script 'Get_Group_Members' executes, reading a text file containing an Active Directory Group name, per line. For each group found, the script identifies the owner of the group, creates a folder named with the owners AD name, and puts a .CSV file in the folder listing all members of the group. Then, I (for now anyway) manually initiate the next Script 'Import_CSV' which pulls all the information into a Sharepoint list for an unrelated process.
Hope that helps understand what's happening. Am I right in assuming I should handle this on the Powershell side, as opposed to the Sharepoint side? If so, am I headin' in the right direction with Grant -SPObjectSecurity?
Thanks!
Update:
Following the link I provided in a comment below, here is what I came up with:
function GrantUserpermission($strOwnerName)
{
[Microsoft.SharePoint.SPUserCollection]$spusers=[Microsoft.SharePoint.SPUserCollection]$web.SiteUsers
[Microsoft.SharePoint.SPUser]$spuser=$spusers[$strOwnerName]
"Strowner name: " + $strOwnerName
# Get the SPWeb object and save it to a variable
$web = Get-SPWeb -identity $WebURL
if ($strOwnerName -ne $null)
{
$sproleass=new-object Microsoft.SharePoint.SPRoleAssignment([Microsoft.SharePoint.SPPrincipal]$spuser)
$folder.BreakRoleInheritance("true")
$sproleass.RoleDefinitionBindings.Add($web.RoleDefinitions["Contribute"])
$folder.RoleAssignments.Add($sproleass);
Write-Host "Permission provided for user ", $strOwnerName
}
else
{
Write-Host "User ""$userName"" was not found in this web!"
}
}
And here, are the error(s) associated with my code:
Full code can be found here: http://pastebin.com/iBpj6V1U
Update #2
#apply permissions to folder
"Strowner name: " + $strOwnerName
function GrantUserpermission($strOwnerName)
{
$web = Get-SPWeb -identity $WebURL
[Microsoft.SharePoint.SPUser]$spuser=$web.EnsureUser($strOwnerName)
"Strowner name in Function: " + $strOwnerName
Updated code #2: http://pastebin.com/DzP1hVce
I ended up realizing, that if I am using Powershell to get information to a .CSV, and then ultimately to Sharepoint, that it doesn't make sense to actually waste time with files, and tap directly into Sharepoint via Powershell.
Here's the code I had used to accomplish this: http://pastebin.com/xRyvXLCB
Special thanks to #TheMadTechnician
I have created a custom template as follows:
Scaffold CustomTemplate View Index
As a result, I now have a local T4 template:
I made some modifications to the template. My question is how to generate code only for the view. I have done the following which works but which in my opinion is overkill since all I want is for my view to be generated.
Scaffold Controller -ModelType Task -ControllerName TasksController -DbContextType TasksDbContext -Repository -Force
I've managed to find out what command to pass:
Scaffold View -ModelType Task -Controller Tasks -ViewName Index -Template Index -Force
What I didn't know about was the -Template string switch.
Guid featureId = new Guid("0af5989a-3aea-4519-8ab0-85d91abe39ff");
ClientContext clientContext = new ClientContext("http://mysite:786/");
Site clientSite = clientContext.Site;
clientContext.Load(clientSite);
FeatureCollection clientSiteFeatures = clientSite.Features;
clientContext.Load(clientSiteFeatures);
clientContext.ExecuteQuery();
// Activate the feature
clientSite.Features.Add(featureId, true, FeatureDefinitionScope.Site);
//clientSiteFeatures.Remove(featureId, false);
clientContext.ExecuteQuery();
MessageBox.Show("Success");
When I am running this code, I am getting the exception: Feature with
id "0af5989a-3aea-4519-8ab0-85d91abe39ff" isn't installed in farm and
can't be added to scope.
I got this feature id from the link http://social.technet.microsoft.com/wiki/contents/articles/7695.list-of-sharepoint-2010-features-id-displayname-and-scopes.aspx
Please guide.
Regards,
Vikrant Raj Behal
FeatureDefinitionScope.None
This activated a web-scoped feature for me.
According to MSDN the FeatureCollection.Add method has the following signature
public Feature Add(
Guid featureId,
bool force,
FeatureDefinitionScope featdefScope
)
which is intended for adding the feature to the collection of activated features and returns the added feature
Parameter FeatureDefinitionScope is used for specifying the feature scope for a feature definition.
At the same time the documentation says:
It must have the value of FeatureDefinitionScope.Site or
FeatureDefinitionScope.Farm
It basically means that the method FeatureCollection.Add does not accept FeatureDefinitionScope.Web value for featdefScope and therefore feature activation with Web scope is not supported.
In your case it seems you are trying to activate feature with scope that is not supported (ex. Web) via CSOM
How to verify feature scope
$feature = get-spfeature featureId
if ($feature -eq $null -or $feature -eq "") {
echo "no feature found with id"
} else {
echo ("feature found. Scope is " + $feature.Scope)
}
In order to activate a feature using the Client Object Model, It has to be deployed using a sandbox solution. Features which are deployed via Farm Solutions cannot be activated through the Client Object Model
FeatureDefinitionScope.None
also works for activating Site Collection features.
The way a feature is added (Sandbox, Farm Solution) does not matter.
How do you work out what type of site a site is in sharepoint 2010?
The by far easiest way to do this is to use PowerShell:
PS> asnp Microsoft.SharePoint.PowerShell
PS> $web = get-spweb http://server/site
PS> $web.WebTemplate
YOURSITEDEF
PS> $web.WebTemplateId
12345
In the object model, SPWeb.WebTemplate will tell you what template was used to the create the site.
If you are just looking to find out the template without writing code, there is a great tool called SharePoint Explorer that you can browse to find the web template and more.
http://spm.codeplex.com/
I found this answer from sharepoint.stackexchange.com useful: "View the HTML source of the page and search for 'g_wsaSiteTemplateId' - this will give you the site template and configuration used to create the site."
In my case the template is a Team Site:
var g_presenceEnabled = true;
var g_wsaEnabled = false;
var g_wsaLCID = 1033;
var g_wsaSiteTemplateId = 'STS#0';
var g_wsaListTemplateId = 119;
The other answers need to be performed on the server (or using remoting for the PowerShell option). That isn't always practical and this option can performed from any machine with web access to the site.