I want to add a App-Webpart like a Custom List which is already in Sharepoint to a Page using PowerShell. How can I do this?
This should do the job...
$WebUrl = 'Your Site\Web Url'
Add-PSSnapin "Microsoft.SharePoint.Powershell"
$SPWeb = Get-SPWeb $WebUrl
$oWebPartPage = $SPWeb.GetFile("SitePages/Test.aspx") # Url of Page where webpart should be added
$oWebPartPage.Checkout()
$oWebPartManager = $oWebPartPage.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$list = $SPWeb.Lists["YourListName"] # Name of the list
$oWebPartView = New-Object "Microsoft.SharePoint.WebPartPages.ListViewWebPart"
$oWebPartView.ListName = $list.ID.ToString("B").ToUpper()
$oWebPartView.ViewType = "None"
$oWebPartView.ViewGuid = ""
$oWebPartManager.AddWebPart($oWebPartView,"RightZone",2)
$oWebPartManager.SaveChanges($oWebPartView)
Related
I currently have a powershell script to loop through a list on an Sharepoint online site.
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("[USERNAME]", (ConvertTo-SecureString "[PASSWORD]" -AsPlainText -Force))
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext("https://[SITENAME].sharepoint.com/sites/Projekter")
$ctx.credentials = $creds
try{
$lists = $ctx.web.Lists
$list = $lists.GetByTitle("Ekstern synkronisering")
$listItems = $list.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$ctx.load($listItems)
$ctx.executeQuery()
foreach($listItem in $listItems)
{
$internal = $listItem["ConnectionString1"]
$external = $listItem["ConnectionString2"]
$folder = $listItem["Mappe"]
$project = $listItem["Project"]
$caseNo = $listItem["Title"]
$doc = New-Object System.Xml.XmlDocument
$doc.Load("C:\Script\PS-Layer2\xmlTemplate.xml")
$ns = New-Object System.Xml.XmlNamespaceManager($doc.NameTable)
$ns.AddNamespace("ns", $doc.DocumentElement.NamespaceURI)
$doc.SelectSingleNode("//ns:dataEntity[#name='Intern site']", $ns).connectionString = $internal+";Authentication=Office365;User Id=$userId;Password=$pwd;"
$doc.SelectSingleNode("//ns:dataEntity[#name='Externt site']", $ns).connectionString = $external+";Authentication=Office365;User Id=$userId;Password=$pwd;"
$doc.Save("C:\Temp\$caseNo - $project - $folder.xml")
}
}
catch{
write-host "$($_.Exception.Message)" -foregroundcolor red
}
This works fine to get the items and modify an XML file..
What I would like to do is, when the XML file has been created, it shall delete the listitem from the sharepoint site, so that it will not be created on the next run again.
Does anyone have a solution on that?
Best regards...
*** I used SharePoint Powershell(2013) to add a webpart to a page
*** Affer added webpart success from powershell, then I go to the page, I saw the webpart be added to the page(I also saw the webpart in Web Part Page Maintenance )
*** But when I show page by edit mode, I don't find the webpart.
*** Can you help me how to add a webpart from powershell but I still can edit the webpart in edit mode of the page on web browser?
Here is my powershell script:
function NewGuid(){
$id = [System.Guid]::NewGuid().ToString()
return "g_" + $id.Replace("-","_")
}
function Add-ContentEditorWebPart()
{
$SiteURL = "https://insiderdev.connorgp.com"
$pageUrl = "https://insiderdev.connorgp.com/ConnorGroup/SitePages/NhanTest1.aspx"
$webpartzone = "Header"
$index = 1
$title = "Content Editor"
$site = new-object Microsoft.SharePoint.SPSite($SiteURL)
$web=$site.OpenWeb()
$webpartmanager = $web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$webpart = new-object Microsoft.SharePoint.WebPartPages.ContentEditorWebPart
$webpart.ChromeType = "None"
$webpart.Title = $title
$webpart.ID = NewGuid
$webpart.FrameType = "None"
$webpart.CatalogIconImageUrl = "/_layouts/15/images/mscontl.gif"
$webpart.PartImageLarge = "/_layouts/15/images/mscontl.gif"
$content = "Test Webpart added"
$docXml = New-Object System.Xml.XmlDocument
$contentXml = $docXml.CreateElement("Content")
$contentXml.set_InnerText($content)
$docXml.AppendChild($contentXml)
$webpart.Content = $contentXml
$webpartmanager.AddWebPart($webpart, $webpartzone, $index)
$web.Close()
$site.Close()
}
Add-ContentEditorWebPart
I am trying to set the description on a Document Library in SharePoint Online using the PnP-PowerShell Commands, but it seems to be very intermittent in working, so I wanted to check what the correct way to do it is?
I create a new library with:
New-PnPList -Template DocumentLibrary -Title "TempLibrary"
Then try to set the Description with:
$l = Get-PnPList -Identity TempLibrary
$l.Description = "My Description Here"
Now clearly that doesn't work since I need to send the changes back with CSOM I assume?
So then I tried the following:
$ctx = Get-PnPContext
$l = Get-PnPList -Identity TempLibrary
$l.Description = "My Description Here"
$ctx.ExecuteQuery()
But this still didn't seem to work.
Any thoughts anyone has on how to do this reliably would be greatly appreciated.
Many thanks,
D.
UPDATE
Eurgh... This seems to work, but is this right? Is it expected? Doesn't feel very PowerShell like...
New-PnPList -Title "Test5" -Template DocumentLibrary
$t = Get-PnPList -Identity Test5
$ctx.Load($t)
$ctx.ExecuteQuery()
$t.Description = "Test5 Description"
$t.Update()
$ctx.ExecuteQuery()
Without loading the list you will not be able to set the description or other properties so the code is correct.
$ctx.Load($t)
Why do you think this is not PS like? :) Curious...
Use the following script. Hope it helps.
Add - PSSnapin Microsoft.SharePoint.PowerShell
function CreateList($spWeb, $listName)
{
$spTemplate = $spWeb.ListTemplates["Document Library"]
$spListCollection = $spWeb.Lists
$spListCollection.Add($listName, $listName, $spTemplate)
}
Function SetDescription($spWeb, $listName)
{
$path = $spWeb.url.trim()
$spList = $spWeb.GetList("$path/Lists/$listName")
$spList.Description = "--Your Desired Description--"
$spList.Update()
}
$siteCollectionUrl = "--Your Site Collection Url--"
$listName = "--Your Desired List Name--"
$spWeb = Get - SPWeb - Identity $siteCollectionUrl
CreateList $spWeb $listName
SetDescription $spWeb $listName
All,
I am trying to create a list using a custom list template that includes content for SharePoint Online using powershell and CSOM. The list template for now is already loaded into the site collection. If I go through the UI, I can create the list on a site using the template including content without issue. If I try to do it through powershell, the list gets created but without the columns and or contents.
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials
if (!$clientContext.ServerObjectIsNull.Value)
{
Write-Host "Connected to SharePoint Online site: '$Url'" -ForegroundColor Green
}
$web = $clientContext.Web
$templates = $clientContext.Site.GetCustomListTemplates($web)
$clientContext.Load($templates)
$clientContext.ExecuteQuery()
$template = $templates | Where-Object{ $_.Name -eq "SomeTemplate" }
$lci = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$lci.Title = "Some List"
$lci.TemplateFeatureId = $template.FeatureId
$lci.TemplateType = $template.ListTemplateTypeKind
$lci.DocumentTemplateType = $template.ListTemplateTypeKind
$lists = $clientContext.Web.Lists;
$clientContext.Load($lists);
$clientContext.ExecuteQuery();
$list = $lists.Add($lci)
$list.Update()
$clientContext.ExecuteQuery()
I can't figure out what is missing, any help would be much appreciated.
I think you have to associate Fields to Content Type (CT) and then add this CT to your List.
Try look at this:
http://www.sharepointfire.com/2016/01/create-new-content-type-sharepoint-online-powershell/
In previous step he created Columns(Fields) which he used in creation:
$columns = "BlogNumber", "BlogText", "BlogUser"
Hope it will help you.
Alex
I Think you have to add the list template before update. It works for me
...
$lci.Title = "Some List"
$lci.TemplateFeatureId = $template.FeatureId
$lci.TemplateType = $template.ListTemplateTypeKind
$lci.DocumentTemplateType = $template.ListTemplateTypeKind
$lci.ListTemplate = $template
...
I am trying to set the default value for a site column.
I found following code to set the default value of a column in library, but I could not find a way to set the site column using MetadataDefaults
add-pssnapin microsoft.sharepoint.powershell -ea silentlycontinue
[Reflection.Assembly]::LoadwithPartialName("Microsoft.Office.DocumentManagement")
$web = get-spweb http://mysitecoll/sites/Nate
$lib = $web.Lists["Shared Documents"]
$metadataDefaults = New-Object -TypeName Microsoft.Office.DocumentManagement.MetadataDefaults -ArgumentList $web
foreach ($oneFolder in $lib.Folders) {
Write-Host ("Folder: {0}" -f $oneFolder.Url)
$folderObject = $web.GetFolder($oneFolder.Url)
$folderDefaultValue = $metadataDefaults.GetFieldDefault($folderObject, "ColumnName")
Write-Host (" Default Value: {0}" -f $folderDefaultValue)
}
$web.dispose()
Help reference for Metadata Default members
https://msdn.microsoft.com/EN-US/library/office/microsoft.office.documentmanagement.metadatadefaults_members.aspx
above code taken from http://blogs.technet.com/b/okoestner/archive/2011/12/01/fetch-default-values-of-folders-with-powershell.aspx
$web = $Global:site.RootWeb
$field = $web.Fields["FieldName"]
$field.DefaultValue = GUID for the value
$field.Update($true)
The above code worked for updating the Site columns default value