Collaboration-2-Go


Lösungen und Software-Entwicklung im Collaboration Umfeld, insbesondere Microsoft SharePoint, Microsoft Exchange und Microsoft Office.

Support


Mobil: +49 (152) 53 97 78 79
Mail: service@collaboration-2-go.de
Weitere Kontaktmöglichkeiten: Kontaktseite
Dekoration: Köln



Export und Import SPWeb with Workflows
Created: 13.11.2018
Categories: PowerShell; SharePoint Development; SharePoint 2016

      If you want to export and import a SharePoint site you can use Export-SPWeb and Import-SPWeb. In most cases these commands work fine.
  If you have SharePoint Designer Workflows in this site, SharePoint adds a folder called "Workflows" and sometime this folder has a property
  called "docid_msft_hier_listid_validate" and a value which is invalid during the import.
  
  German error message:
  Import-SPWeb : Von der Zeichenfolge dargestellte DateTime liegt außerhalb des gültigen Bereichs.
  
  The following command changes the value and an import can be executed without problems:

  
$web = Get-SPWeb -Identity https://sharepoint/sites/PowerShellTests
$folder = $web.Folders["Workflows"]
$property = $folder.Properties["docid_msft_hier_listid_validate"]

$dt = Get-Date -Year 2100 -Month 12 -Day 31
$folder.Properties["docid_msft_hier_listid_validate"] = $dt
$folder.Update()
$web.Update()
  

    

Send us a Comment!


Rename Internal Document Library Name
Created: 10.11.2018
Categories: PowerShell; SharePoint Development; SharePoint 2016

      If you want to rename a SharePoint library you can use the library configuration.
  If you want to rename the internal name you could use SharePoint Designer. Unless you have more than 5.000 items (which is a bad idea anyway):
  
  Server error: The attempted operation is prohibited because it exceeds the list view treshold enforced by the administrator.

  Ribbon
  
  But you can change the internal name using PowerShell:
  
      
    $url = "https://sharepoint/sites/PowerShellTests"
    $web = Get-SPWeb $url
    $lib = $web.GetList($web.Url + "/oldName")
    $rootFolder = $lib.RootFolder
    $rootFolder.MoveTo($web.Url + "/newName")
  

    

Send us a Comment!


Re-Apply Quotas
Created: 05.10.2017
Categories: PowerShell; SharePoint Development; SharePoint 2016

      If you change a quota template you have to re-apply the template. In this case it was very easy since all sites have the same quota (personal My Sites).

      
$wa = $get-spwebapplication "my site host url"
$wa.Sites | foreach-object {
  if ( $_.Url.StartsWith("my site host url/personal") ) {
    Set-SPSite -Identity $_.Url -QuotaTemplate "Personal Site"
  }
}

    

Send us a Comment!


Send Mail
Created: 04.10.2017
Categories: PowerShell

      If you want to send a mail with an attachment using PowerShell you can you this code:

      
function SendSmtpMail($server, $sender, $recipients, $subject, $body, $file)
{
    $msg = new-object Net.Mail.MailMessage
    $smtpServer = new-object Net.Mail.SmtpClient($server)
    $msg.From = $sender
    $msg.Subject = $subject
    $msg.Body = $body

    foreach ($recipient in $recipients)
    {
        $msg.To.Add($recipient)
    }

    if ($file -ne "")
    {
        $att = new-object Net.Mail.Attachment($file)
        $msg.Attachments.Add($att)
    }

    $smtpServer.Send($msg)

    if ($file -ne "")
    {
      $att.Dispose()
    }
}

$smtpServer = "my smtp server"
$file = "path to file"
$recipients = @( "user1@test.de", "user2@test.de" )
$sender = "sender@mydomain.com"
$subject = "My Subject"
$body = "My Mail Text"

SendSmtpMail $smtpServer $sender $recipients $subject $body $file

    

Send us a Comment!


Check if File is Locked
Created: 20.09.2017
Categories: PowerShell

      If you want to check if a file is locked you can you this snippet:

      
      $locked = $true
      while ($locked)
      {
          try
          {
              $file = New-Object System.IO.FileInfo $Path
              [IO.File]::OpenWrite($file).close();
              $locked = $false
          }
          catch
          {
              # file is locked by a process.
              Start-Sleep -s 1000
          }
      }
      

    

Send us a Comment!


Empty Popularity Trends and Most Popular Items
Created: 24.08.2017
Categories: SharePoint Development; PowerShell; SharePoint 2016

      In case the popularity trends report and the most popular items report are empty you can check (and re-eanble) the event receivers using PowerShell:

      Get receivers:
      
      $analytics = Get-SPUsageDefinition | where {
        $_.Name -like "Analytics*" }
      $pageRequests = Get-SPUsageDefinition | where {
        $_.Name -like "Page Requests" }
      


      Check receivers:
      
      $analytics.Receivers.Count
      $pageRequests.Receivers.Count
      


      If the result is 0 you can re-enable the receivers
      (remove line breaks)
      
      if ($analytics.Receivers.Count -eq 0)
      {
        $analytics.Receivers.Add(
          "Microsoft.Office.Server.Search.Applications,
          Version=16.0.0.0, Culture=neutral,
          PublicKeyToken=71e9bce111e9429c",
          "Microsoft.Office.Server.Search.Analytics.Internal.
          AnalyticsCustomRequestUsageReceiver")
      }
      if ($analytics.EnableReceivers -eq $false)
      {
        $analytics.EnableReceivers = $true
        $analytics.Update()
      }

      if ($pageRequests.Receivers.Count -eq 0)
      {
        $pageRequests.Receivers.Add(
          "Microsoft.Office.Server.Search.Applications,
          Version=16.0.0.0, Culture=neutral,
          PublicKeyToken=71e9bce111e9429c",
          "Microsoft.Office.Server.Search.Analytics.Internal.
          ViewRequestUsageReceiver")
      }
      if ($pageRequests.EnableReceivers -eq $false)
      {
        $pageRequests.EnableReceivers = $true
        $pageRequests.Update()
      }
      

    

Send us a Comment!


Download WSP Solution File from SharePoint
Created: 26.04.2017
Categories: SharePoint Development; PowerShell

      Download WSP Solution File from SharePoint

      
      $farm = Get-SPFarm
      $file = $farm.Solutions.Item("MySolution.wsp").SolutionFile
      $file.SaveAs("c:\temp\MySolution.wsp")
      

    

Send us a Comment!


Activate SharePoint TimerJob using PowerShell
Created: 20.02.2017
Categories: SharePoint Development; PowerShell

      Activate SharePoint TimerJob using PowerShell

      
      # Configuration
      $app = Get-SPWebapplication "web app url"
      $jobname = "my job name"
      $siteurl = "site url will be used within the job"

      # Install
      $job = New-Object MyNamespace.MyClass(
        $jobname, $app, $siteurl)
      $job.Schedule = [Microsoft.SharePoint.SPSchedule]::
        FromString("daily between 01:00:00 and 01:00:00")
      $job.Update()
      Restart-Service SPTimerV4

      # Check
      (Get-SPTimerJob | where-object{$_.Name -eq $jobname)}) | fl
      

    

Send us a Comment!


Access Personal Views / Do something on behalf
Created: 04.01.2016
Categories: SharePoint Development; PowerShell

      Impersonate and access personal views of somebody else.

      Code:
      
      # enter valid values
      $url = "https://sharepoint.c2go.net/sites/test"
      $listName = "MyListName"
      $login = "MyLogin"

      $web = Get-SPWeb $url
      $userid = ($web.Allusers | where-object
        { $_.LoginName -eq $login }).ID
      $user = $web.AllUsers.GetByID($userid)
      $token = $user.UserToken;
      $impSite = New-Object Microsoft.SharePoint.SPSite
        ($web.Url, $token);
      $impWeb = $impSite.OpenWeb()
      $impList = $impWeb.Lists[$listName]
      $impList.Views
      

    

Send us a Comment!


Upload Document to a SharePoint Library
Created: 15.03.2015
Categories: PowerShell; SharePoint 2013

Upload Document to a SharePoint Library:


$destinationUrl = "https://sharepoint/sites/teamsite/documents"
$file = get-childitem "e:\tmp\document.txt"

$webclient = New-Object System.Net.WebClient
$webclient.UseDefaultCredentials = $true
$webclient.UploadFile($destinationUrl + "/" + $file.Name, "PUT", $file.FullName)


In case you don't want to use integrated security you have to set the credentials.
    

Send us a Comment!


Restart Worker Processes and SPTimerV4 in Farm
Created: 13.12.2014
Categories: PowerShell; SharePoint 2013

Restart IIS worker processes and SPTimerV4 in SharePoint Farm


add-pssnapin microsoft.sharepoint.powershell
$spserver = get-spserver | ?{$_.role -eq "Application"}
foreach ($server in $spserver)
{
  $name = [string]::concat("\\", $server.name)
  write-host "Performing IIS Reset on Server:"$server.name
  iisreset $server.Name

  write-host "Stopping SPTimerV4 on Server:"$server.name
  $stat = sc.exe $name stop sptimerv4
  $stat = sc.exe $name query sptimerv4
  while ($stat -match "STOP_PENDING")
  {
    write-host "Stopping"
    $stat = sc.exe $name query sptimerv4
    start-sleep 4
  }

  write-host "Starting SPTimerV4 on Server:"$server.name
  $stat = sc.exe $name start sptimerv4
  $stat = sc.exe $name query sptimerv4
  while (-not $stat -match "RUNNING")
  {
    write-host "Starting"
    $stat = sc.exe $name query sptimerv4
    start-sleep 4
  }

  write-host "SPTimerV4 started on Server:"$server.name
}

    

Send us a Comment!


Remove duplicate Active Directory Group Memberships
Created: 10.12.2014
Categories: PowerShell

Once I had to remove all users in an Active Directory group in case they are also member of a second group.


# if I have to talk to a different but truested domain
$dc = "my_domain_controller"

# my groups
$group1 = "my_first_group"
$group2 = "my_second_group"

diff (Get-ADGroupMember -Identity $group1 -Server $dc) (Get-ADGroupMember -Identity $group2 -Server $dc) -Property 'distinguishedName' -IncludeEqual | ?{ $_.sideIndicator -eq "==" } | foreach-object { write-host $_.distinguishedName ; Remove-ADGroupMember -Identity $group1 -Server $dc -Members $_.distinguishedName -Confirm:$false }

    

Send us a Comment!


Add Followers to a SharePoint 2013 Site
Created: 11.01.2014
Categories: SharePoint 2013; SharePoint Development; PowerShell

Add Followers to a SharePoint 2013 Site using PowerShell:

$followedSite = „https://sharepoint/sites/news“
$loginName = „roloff“

$profile = $upm.GetUserProfile($loginName)
$manager = New-Object Microsoft.Office.Server.Social.SPSocialFollowingManager($profile)

$actorInfo = New-Object Microsoft.Office.Server.Social.SPSocialActorInfo
$actorInfo.ContentUri = $followedSite
$actorInfo.ActorType = 2 # SPSocialActorType.Site
$manager.Follow($actorInfo)

    

Send us a Comment!


Create SharePoint 2013 MySites
Created: 10.01.2014
Categories: SharePoint 2013; SharePoint Development; PowerShell

Create SP2013 MySites using an AD group and PowerShell:

$adGroup = ""  # AD Group with MySite users
$siteURL = ""  # any local SharePoint Site will do

$members = Get-ADGroupMember -Identity $adgroup -Recursive | sort name

# get UserProfileManager
$serviceContext  = Get-SPServiceContext -site $siteURL -ErrorAction Stop
$upm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext) -ErrorAction Stop

foreach ($member in $members)
{
if ($upm.UserExists($member.SamAccountName))
{
  $profile = $upm.GetUserProfile($member.SamAccountName)
  $perssite = $profile.PersonalSite

  if ($perssite -eq $null)
  {
    # either this
    $profile.CreatePersonalSite(1031)  # should work in most cases
    # or this
    # $profile.CreatePersonalSiteEnque($true)  # will work in all cases; gets language from mysite host

    $perssite = $profile.PersonalSite
    if ($perssite -eq $null)
    {
      # do some error handling
    }
  }
}
}

    

Send us a Comment!


SharePoint Database Size
Created: 09.01.2014
Categories: SharePoint 2013; SharePoint Development; PowerShell

SharePoint Database Size using PowerShell:

Get-SPDatabase |
  where-object { $_.TypeName -eq "Content Database" } |
  select Displayname,
  @{Name="Mbytes";Expression={$_.DiskSizeRequired/ 1Mb}}


Please note: TypeName is localized, e.g. use "Inhaltsdatenbank" in a German farm.
    

Send us a Comment!


Rename SharePoint MySite Blog
Created: 16.11.2013
Categories: SharePoint 2013; SharePoint Development; PowerShell

Want to rename your MySite blogs using PowerShell since all MySite blog names are "Blog"?


# URL to your MySite web application
$MySiteWebUrl = "https://mysite.collaboration-2-go.net"
      
# where to find users in the AD
$searchbase="DC=collaboration-2-go,DC=net"
      
# required if server domain and user domain are different
$dc="my_domain_controller"
      
# required if server language is English and
# MySite language is different
$culture = "de-DE"

$webapp = Get-SPWebApplication -identity $MySiteWebUrl
[System.Threading.Thread]::CurrentThread.CurrentUICulture =
  $culture
        
foreach ($site in $webapp.Sites)
{
  # Get Active Directory Information
  $login = $site.Owner.LoginName
  if ($login.contains("\"))
  {
    $login = $login.substring($login.indexof("\") + 1)
  }
        
  $ldapfilter = "(SamAccountName=" + $login + ")"
  $aduser = Get-ADUser -LDAPFilter $ldapfilter
    -SearchBase $searchbase -Server $dc
  $displayName = $aduser.GivenName + " " + $aduser.Surname
      
  if ($displayName -eq " ")
  {
    $displayName = $site.owner.DisplayName
  }
      
  if ($site.RootWeb.WebTemplate -eq "SPSPERS") # ignore MySite host
  {
    # more sophisticated approach is possible but
    # I'm not sure about the blog URL
    foreach ($web in $site.AllWebs)
    {
      if (($web.WebTemplate -eq "BLOG") -and
        ($web.Title -eq "Blog"))
      {
        $title = "Blog von " + $displayName
        $web.Title = $title
        $web.Update()
      }
          
      $web.Dispose()
    }
  }
        
  $site.Dispose()
}

      
If you don't set the culture SharePoint changes to English title only.
    

Send us a Comment!


SharePoint Blog Layout
Created: 16.11.2013
Categories: SharePoint 2013; SharePoint Development; PowerShell

Want to apply different blog layouts using PowerShell?
      
Sample: Standard Layout:
Blog Beitrag Layout Standard

Sample: Framed Layout:
Blog Beitrag Layout Umrahmt

$web = Get-SPWeb https://sharepoint/sites/blog
write-host $web.Properties["ms-blogs-skinid"]
$web.Properties["ms-blogs-skinid"] = 1
$web.Properties.update()

Valid values for ms-blogs-skinid: 1, 2, 3
    

Send us a Comment!


How to create Exchange Public Folders for Appointments
Created: 04.10.2011
Categories: Exchange Development; Exchange; PowerShell

      Unfortunately the PowerShell cmdlet New-PublicFolder doesn’t allow different types for public folders. But with a small trick you can set the type later. The code requires CDOEX that runs locally only.

      PowerShell Code:
      
      $FolderPath = "/TEMP"
      $NewFolder = "CalendarTest"
      $PFRoot = "file://./backofficestorage/litwareinc.com/Public Folders"

      # Use PowerShell cmdlet to create new folder
      # (depending upon whether a root folder or not)
      if ($FolderPath -ne "")
      {
        $f = $FolderPath -replace("/", "\")
        New-PublicFolder -Name $NewFolder -Path $f
      }
      else
      {
        New-PublicFolder -Name $NewFolder
      }

      # Use ADO to change the folder type
      $o=New-Object -comobject ADODB.Record
      $updated=$false
      $timeout=60
      while (($updated -eq $false) -and ($timeout -gt 0))
      {
        $o.Open($PFRoot + $FolderPath + "/" +
         $NewFolder, "", 3, -1, -1, "", "")
        foreach($item in $o.Fields)
        {
        if($item.Name -eq
          "http://schemas.microsoft.com/exchange/outlookfolderclass")
        {
          $updated=$true
          $item.Value="IPF.Appointment"
        }
      }

      $o.Fields.Update()
      $o.Close()

      if ($updated -eq $false)
      {
      Start-Sleep -s 1
      $timeout --
      }
      }
      

      Tested IPF Types: IPF.Post, IPF.Contact, IPF.Appointment, IPF.Task
      Tested Exchange Version: Exchange 2007
    

Send us a Comment!