ĐÀO TẠO DOANH NGHIỆP : SỞ KHOA HỌC CÔNG NGHỆ TỈNH ĐỒNG NAI

ENTERPRISE TRAINING: DONG NAI DEPARTMENT OF SCIENCE AND TECHNOLOGY.

HÌNH ẢNH TẬP HUẤN LỚP SHAREPOINT WORKFLOW VÀ KIẾN TRÚC SHAREPOINT

PHOTOS OF SHAREPOINT WORKFLOW AND ARCHITECTURE CLASS.

HÌNH ẢNH TẬP HUẤN LỚP SHAREPOINT WORKFLOW VÀ KIẾN TRÚC SHAREPOINT

PHOTOS OF SHAREPOINT WORKFLOW AND ARCHITECTURE CLASS.

Showing posts with label Windows PowerShell (ISE). Show all posts
Showing posts with label Windows PowerShell (ISE). Show all posts

Friday, November 6, 2015

Sharepoint PowerShell List of all site collections in Web Application

$xl=New-Object -ComObject "Excel.Application"
$xl.Visible=$True
$xl.Workbooks.Add()
$ws=$xl.ActiveWorkbook.ActiveSheet
$cells=$ws.Cells
$i = 1
$sites = Get-SPSite -Limit All -WebApplication http://pc362/
foreach ($site in $sites)
{
 $cells.item($i,1)= invoke-command { $site.URL }
 $cells.item($i,2)= invoke-command { $site.Owner.UserLogin }
 $cells.item($i,3)= invoke-command { $site.SecondaryContact.UserLogin }
 $cells.item($i,4)= invoke-command { $site.WebApplication.DisplayName }
 $cells.item($i,5)= invoke-command { $site.Usage.Storage/1MB }
 $cells.item($i,6)= invoke-command { $site.ContentDatabase.Name }
 $cells.item($i,7)= invoke-command { $site.Quota.Storagemaximumlevel }
 $cells.item($i,8)= invoke-command { $site.RootWeb.SiteUsers.Count }
 $cells.item($i,9)= invoke-command { $site.Usage.Hits }
 $cells.item($i,10)= invoke-command { $site.Usage.Bandwidth }
 $i++
}

Or

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
#Configure the location for the output file
$Output="C:\Output.csv";
"Site URL"+","+"Owner Login"+","+"SecondaryContact Login"+","+"WebApplication DisplayName"+","+"Quota Limit (MB)"+","+"Total Storage Used (MB)"+","+"Site Quota Percentage Used"+","+"ContentDatabase"+","+"SiteUsers"+","+"UsageHits"+","+"UsageBandwidth" | Out-File -Encoding Default -FilePath $Output;
#Specify the root site collection within the Web app
$Siteurl="https://your web application";
$Rootweb=New-Object Microsoft.Sharepoint.Spsite($Siteurl);
$Webapp=$Rootweb.Webapplication;
#Loops through each site collection within the Web app, if the owner has an e-mail address this is written to the output file
Foreach ($Site in $Webapp.Sites)
{if ($Site.Quota.Storagemaximumlevel -gt 0) {[int]$MaxStorage=$Site.Quota.StorageMaximumLevel /1MB} else {$MaxStorage="0"};
if ($Site.Usage.Storage -gt 0) {[int]$StorageUsed=$Site.Usage.Storage /1MB};
if ($Storageused-gt 0 -and $Maxstorage-gt 0){[int]$SiteQuotaUsed=$Storageused/$Maxstorage* 100} else {$SiteQuotaUsed="0"};
$Web=$Site.Rootweb; $Site.Url + "," + $Site.Owner.UserLogin + "," + $Site.SecondaryContact.UserLogin + "," +$Site.WebApplication.DisplayName + "," +$MaxStorage+","+$StorageUsed + "," + $SiteQuotaUsed+ "," + $Site.ContentDatabase.Name+ "," + $Site.RootWeb.SiteUsers.Count+ "," + $Site.Usage.Hits+ "," + $Site.Usage.Bandwidth | Out-File -Encoding Default -Append -FilePath $Output;$Site.Dispose()};

Thursday, October 22, 2015

PowerShell copy list item to another list with same structure

Insert item from source list

$web = Get-SPWeb http://yourURL
$lista =$web.Lists["Source List Name"]
$listb =$web.Lists["Target List Name"]
$items = $lista.items
foreach ($item in $items) {
write-host -foregroundcolor yellow $item["Title"]
$newitem= $listb.items.Add()
$newitem["Title"] = $item["Title"]
$newitem["MultiLine"] = $item["MultiLine"]
$newitem["Number"] = $item["Number"]
$newitem["HyperLink"] = $item["HyperLink"]
$newitem["Choice"] = $item["Choice"]
$newitem["Created"] = $item["Created"]
$newitem["Author"] = $item["Author"]
$newitem["Modified"] = $item["Modified"]
$newitem["Editor"] = $item["Editor"]
$newitem.update()
start-sleep 1
}
$web.dispose
start-sleep 10

Powershell sharepoint get item by id

$web = Get-SPWeb http://yourURL
$lista =$web.Lists["Source List Name"]
$listb =$web.Lists["Target List Name"]
$items = $lista.GetItemById(523)
foreach ($item in $items) {
write-host -foregroundcolor yellow $item["ID"]
write-host -foregroundcolor yellow $item["Title"]
start-sleep 1
}
$web.dispose
start-sleep 10

Update Item by ID

$web = Get-SPWeb http://yourURL
$lista =$web.Lists["Source List Name"]
$listb =$web.Lists["Target List Name"]
$items = $lista.GetItemById(523)
foreach ($item in $items) {
write-host -foregroundcolor yellow $item["Title"]
$newitem= $listb.GetItemById(524)
$newitem["Title"] = $item["Title"]
$newitem["MultiLine"] = $item["MultiLine"]
$newitem["Number"] = $item["Number"]
$newitem["HyperLink"] = $item["HyperLink"]
$newitem["Choice"] = $item["Choice"]
$newitem["Created"] = $item["Created"]
$newitem["Author"] = $item["Author"]
$newitem["Modified"] = $item["Modified"]
$newitem["Editor"] = $item["Editor"]
$newitem.update()
start-sleep 1
}
$web.dispose
start-sleep 10

Thanks.

Thursday, October 1, 2015

Listing All SharePoint Server Features of Web Application dynamic by power shell

Listing All SharePoint Server Features of Web Application dynamic by power shell
1. Writing the function to export all features of web application with parameter
function Get-SPFeatureActivated
{
[CmdletBinding()]
param(
    [parameter(Mandatory=$true)][string]$Url,#Input your URL of web application
    [parameter(Mandatory=$true)][string]$Path,#Input your file name which you want to export like (c:\features.csv)
    [Parameter(position = 1, valueFromPipeline=$true)]#Default para
    [Microsoft.SharePoint.PowerShell.SPFeatureDefinitionPipeBind]   
    $Identity
)#end param
    Begin
    {       
        $results = @()
       
        $params = @{}
    }
    Process
    {
        if([string]::IsNullOrEmpty($Identity) -eq $false)
        {
            $params = @{Identity = $Identity
                        ErrorAction = "SilentlyContinue"
            }
        }   

        # Get web application URL
    $webApplication = Get-SPWebApplication -Identity $Url
        foreach($webApp in $webApplication)
        {
            $results += (Get-SPFeature -WebApplication $webApp -Limit All @params |
                             % {Add-Member -InputObject $_ -MemberType noteproperty -Name Url -Value $webApp.Url -PassThru} |
                             Select-Object -Property Scope, DisplayName, Id, Url)

            # check site collection features in current web app
            foreach($site in ($webApp.Sites))
            {
                $results += (Get-SPFeature -Site $site -Limit All @params |
                                 % {Add-Member -InputObject $_ -MemberType noteproperty -Name Url -Value $site.Url -PassThru} |
                                 Select-Object -Property Scope, DisplayName, Id, Url)
                                
                $site.Dispose()

                # check site features in current site collection
                foreach($web in ($site.AllWebs))
                {
                    $results += (Get-SPFeature -Web $web -Limit All @params |
                                     % {Add-Member -InputObject $_ -MemberType noteproperty -Name Url -Value $web.Url -PassThru} |
                                     Select-Object -Property Scope, DisplayName, Id, Url)

                    $web.Dispose()
                }
            }
        }
    }
    End
    {
        $results > $Path
    }
}

2. Call function then input the value via parameter in above function

3. The result is all features is exported to csv file 
 Thanks.

Wednesday, March 14, 2012

How to Create Fields to Sharepoint List by Windows PowerShell ISE


Below is list data type of fields

TypeName
--------
Counter
Text
Note
Choice
MultiChoice
GridChoice
Integer
Number
ModStat
Currency
DateTime
Lookup
Boolean
Threading
ThreadIndex
Guid
Computed
File
Attachments
User
URL
Calculated
Recurrence
CrossProjectLink
ContentTypeId
MultiColumn
LookupMulti
UserMulti
WorkflowStatus
AllDayEvent
WorkflowEventType
Create new list for lookup with name is Departments then add new item as follows
Create new custom list to create new field for this custom list
Copy this segment code and paste to here

$SPSite = New-Object Microsoft.SharePoint.SPSite("http://quochung-axioo:90");
#Open you web
$OpenWeb = $SpSite.OpenWeb();
#Open Your List
$List = $OpenWeb.Lists["MyCustomList"];
#Add User Field to list
$List.Fields.Add("UserField", "User", $User)
#Add TextField to list
$List.Fields.Add("TextField", "Text", $Text)
#Add UrlHyperField Field to list
$List.Fields.Add("UrlHyperField","URL",$false)
$List.Fields[“UrlHyperField”].Description = “My UrlHyperField Field”
#Add DateTime Field to list
$List.Fields.Add("DateTimeField","DateTime",$false)
$List.Fields[“DateTimeField”].Description = “My DateTime Field”
#Define Choice Field
$Choices = New-Object System.Collections.Specialized.StringCollection
$Choices.Add("First Choice")
$Choices.Add("Second Choice")
$Choices.Add("Third Choice")
#Add Choice Field to list
$List.Fields.Add("ChoiceField",       [Microsoft.SharePoint.SPFieldType]::Choice,     $FALSE,          $FALSE,    $Choices)
#Get List Item from list Lookup (Departments)
$LookupList = $OpenWeb.Lists["Departments"]
$LookupListID = $LookupList.ID
#Add Field Lookup to list
$List.Fields.AddLookup("LookupField",$LookupListID,$FALSE)
#Update view
$Views = $List.Views["All Items"]
$Views.ViewFields.Add("UserField")
$Views.ViewFields.Add("TextField")
$Views.ViewFields.Add("UrlHyperField")
$Views.ViewFields.Add("DateTimeField")
$Views.ViewFields.Add("ChoiceField")
$Views.ViewFields.Add(“LookupField”)
$Views.Update()
$OpenWeb.Dispose();
$SPSite.Dispose()
Run script Completed
Open your list and see result as follows
Try Add new item and see result as follows:

How to create Content type – Site Column – Add site column exists to content type by Windows PowerShell ISE


Create content types

Open Windows PowerShell ISE  | copy this segment code and paste to here then click Run script

$spWeb = Get-SPWeb http://quochung-axioo:90
$spWeb.AvailableContentTypes | Select Name
$parent = $spWeb.AvailableContentTypes["Document Sets"]
$contentType =  New-Object Microsoft.SharePoint.SPContentType -ArgumentList @($parent, $spWeb.ContentTypes, "Finance")
$contentType.Group = "My Content Type"
$contentType.Description = "Standard Documents"
$spWeb.ContentTypes.Add($contentType)
$spWeb.Update()
$spWeb.Dispose()

Script run Completed
Open your site | Go to Site Actions | Site Settings | In category “Gallaries” click to “Site content types” link and see result as follows:

Create Site Column

New PS1 file | copy this segment code and paste to here then click Run script
#Assumes that you are creating the site column in the top-level site of a site collection
#Script can also be used to create a site column in the Content Type Hub
#Get site and web object
$site = Get-SPSite -Identity http://quochung-axioo:90
$web = $site.RootWeb

#Assign fieldXML variable with XML string for site column
$fieldXML = '<Field Type="DateTime"
Name="TestDate"
Description="This is a test date column."
DisplayName="Test Date"
StaticName="TestDate"
Group="My Test Custom Columns"
Hidden="FALSE"
Required="FALSE"
Sealed="FALSE"
ShowInDisplayForm="TRUE"
ShowInEditForm="TRUE"
ShowInListSettings="TRUE"
ShowInNewForm="TRUE"></Field>'
#Output XML to console
write-host $fieldXML
#Create site column from XML string
$web.Fields.AddFieldAsXml($fieldXML)

#Example of changing the site column settings after creation
#Configure Test Date column to be Date Only instead of Date & Time
$dtField = $web.Fields["Test Date"]
$dtField.DisplayFormat = "DateOnly"
$dtField.Update()
#Dispose of Web and Site objects
$web.Dispose()
$site.Dispose()
Script run Completed
Open your site | Go to Site Actions | Site Settings | In category “Gallaries” click to “Site columns” link and see result as follows:

Add site column exists to content type

Open your site | Go to Site Actions | Site Settings | In category “Gallaries” click to “Site content types” then click to “Finance” content type, you have not seen any content type was added
Open your site | Go to Site Actions | Site Settings | In category “Gallaries” click to “Site columns” link and see result as follows:
Copy and paste this segment code to here

$site = Get-SPSite -Identity http://quochung-axioo:90
$web = $site.RootWeb

$ct=$web.ContentTypes["Finance"];
$fieldAdd=$web.Fields["AddSiteColumnToContentType"]
$fieldLink=New-Object Microsoft.SharePoint.SPFieldLink($fieldAdd)
$ct.FieldLinks.Add($fieldLink);
$ct.Update()

$web.Dispose()
$site.Dispose()
Run script completed
Open your site | Go to Site Actions | Site Settings | In category “Gallaries” click to “Site content types” then click to “Finance” content type, you have seen any content type was added

Tuesday, March 13, 2012

How to Create Sharepoint List And Add new – Edit – Delete Sharepoint Item list by Windows PowerShell ISE


Open Windows PowerShell ISE and paste this segment code to here then click Run Script
$web = get-spweb http://quochung-axioo:90
$web.Lists.Add(“PowerShellCustomList”,”Create SP List by Power Shell”,$web.ListTemplates["Custom List"])
$list = $web.Lists["PowerShellCustomList"]
$list.Fields.Add("LastName","Text","DO")
$list.Fields.Add("FirstName","Text","QuocHung")
$list.Fields.Add("Age","Number",25)
Run script succeeded
Open your sharepoint site and see your list was created
Edit view as follows:
Result as:
On ISE, new file .ps1 and paste this segment code to here then click run script
$spAssignment = Start-SPAssignment
$mylist = (Get-SPWeb -identity http://quochung-axioo:90 -AssignmentCollection $spAssignment).Lists["PowerShellCustomList"]
$newItem = $mylist.Items.Add()
$newItem["Title"] = “Added by Powershell”
$newItem["LastName"] = “QuocHung”
$newItem["FirstName"] = “Do”
$newItem["Age"] = 25
$newItem.Update()
Stop-SPAssignment $spAssignment
Go back your sharepoint list and see Item is added
Continue, New .PS1 and paste this segment code to here then click run script

$SPAssignment = Start-SPAssignment
$SPWeb = Get-SPWeb http://quochung-axioo:90 -AssignmentCollection $spAssignment
$SPList = $SPWeb.Lists["PowerShellCustomList"]
$SPItem = $SPList.GetItemById("1")
$SPItem["Title"] = "Edited by Powershell"
$SPItem.Update()
Stop-SPAssignment $SPAssignment
Go back your sharepoint list and see Item is Edited
Continue, New .PS1 and paste this segment code to here then click run script

$spList = Get-SPList -url "http://quochung-axioo:90/Lists/PowerShellCustomList"
$spListItem = $spList.GetItemById(1)
$spListItem.Delete()

Go back your sharepoint list and see Item is Deleted

How to run script sharepoint on powershell

Open Windows PowerShell ISE then paste segment code Get-SPSite to Command pane up  (View > Go to Command Pane)  as follows:
You see error because local user PowerShell ISE profile doesn’t create
Copy segment code and paste to Command pane up (View > Go to Command Pane) as follows:
# Creates a local user PowerShell ISE profile
if (!(test-path $profile ))
{new-item -type file -path $profile -force}
# opens it for edit
psEdit $profile
Completed
Save it (click to Icon Save)
Close and Open Again
Paste segment code “Get-SPSite” to Command pane up  (View > Go to Command Pane)  as follows:
Script run succeeded
You can copy “Get-SPSite” and paste to Script Pane (View -> Script Pane) then click “Run Script” Icon
Result as follows:
Save script
Exit ISE
Start ISE then open file .ps1
Choose your file
UI as follows
Result as follows: