Showing posts with label sharepoint. Show all posts
Showing posts with label sharepoint. Show all posts

Friday, August 5, 2016

Powershell Script to Get SharePoint SiteUsage information


Hi Readers,

There was a requirement to get the sites that were not used , so that we can archive them.
I found some blogs with getting last modified information using the database but the issue is it wasn's correct as all the sites had a modified date which is very recent. This may be due to search crawls.

I came up with some functions that will be useful, here the last modified date and time and the people with full control access will be listed. the purpose of this script will be informing the people with full permissions.

#Get the last Modified information of a site
function GetSiteModifiedInformation
{
    #First Argument will be the site collection url
 $siteUrl=$args[0]
    #Ouput file name or path
 $filename=$args[1]

    #Check if file url is provided
 if($siteUrl -eq $null -or $siteUrl -eq '')
 {
  Write-Host "Please provide a siteUrl" -ForegroundColor Red
  return;
 }
 
    
    Write-Host "Getting Site Information" -ForegroundColor Yellow
    #Get Site using url
    $web=Get-Spweb $siteUrl

    Write-Host "Site Name : "$web.Title
    Write-Host "Created : "$web.Created
    $maxDate=GetLastModifiedListTime $web
    
    #Iterate subsites and get info from subsites
    foreach($spsite in $web.Webs)
    {
        $val=GetLastModifiedListTime $spsite
        
        if($val -gt $maxDate)
        {
            $maxDate = $val
        }
    }

    #Get all users of the site
    $userString='';
    $users = $web.siteusers
    foreach ($user in $users) {
        #Filter users with full control
        if ($web.DoesUserHavePermissions($user,[Microsoft.SharePoint.SPBasePermissions]::FullMask)) {
            $userString= $userString+$user.Name+';'
        }
    }

    Write-Host "Last Modified "$maxDate
    Write-Host $userString
    $name=$web.Title;

    #Add site information to file name
    Add-Content $filename "$siteUrl,$name,$maxDate,$userString"
    $web.Dispose()
}

function GetLastModifiedListTime
{
    #A SPWeb Object is required here
    $site= $args[0]

    #create a date variable with minimum date value
    $date = [DateTime]::MinValue
 
 foreach($list in $web.Lists)
 {
        #Get the most recently updated sharepoint list
  if($date -lt $list.LastItemModifiedDate -and !$list.Hidden)
  {
   $date=$list.LastItemModifiedDate
  }

 }
    return $date
}

#Another helper function that can be used to input a text file
#with sites urls
function GetAllSiteInfo
{
    $siteList =$args[0]
    $output=$args[1]
    $sites= Get-Content $siteList
    foreach($site in $sites)
    {
        GetSiteModifiedInformation $site $output
    }
}


Happy Coding
Guruparan Giritharan

Monday, May 30, 2016

ID3242: The security token could not be authenticated or authorized.

Hello Readers,

We had to setup a new content source in our search and suddenly we got this error while going to the Content sources in Search Service Application.

I googled n didn't find much information so i went through the ULS logs and found these entries.

 An operation failed because the following certificate has validation errors:  Subject Name: CN=###############.com Issuer Name: CN=###############.com Thumbprint: 9238C86F4CF817870AFAB778E9E5E140D7ADE82F  Errors:   The root of the certificate chain is not a trusted root authority..

STS Call: Failed to issue new security token. Exception: System.IdentityModel.Tokens.SecurityTokenValidationException: ID4257: X.509 certificate 
'CN=###############.com' validation failed by the token handler.

An exception occurred when trying to issue security token: ID3242: The security token could not be authenticated or authorized..

the actual issue was that one of the certificates were not added to 'SPTrustedRootAuthority'

Fixing this is simple

$cert = Get-PfxCertificate C:\###############.pfx
New-SPTrustedRootAuthority -Name "###############" -Certificate $cert

If you have a certificate that requires a password
Use IE and go to 'Central Admin'/_admin/ManageTrust.aspx and upload the certifcate.

That's how i got the issue fixed.
Hope it helps :)

Happy Coding
Guruparan Giritharan

Monday, June 2, 2014

Upgradable Sharepoint Webtemplates - Introduction

Hi Readers,

This post is about creating web templates that are upgradable, the method I'm using here is using several features to upgrade the site template.

Let's see the requirement for this, think that your client has a requirement for creating a team site which has a template that is different from the out of the box team site, this may include the contents of the site or even the structure of the site. Your client may ask for a set of lists and libraries that are to be created when a new site is created and the site may have pages that are to be created and webparts are to be placed at those pages.

It may sound simple and you may think that creating a simple site and saving the web template will do the trick, well at some times it will but what if your client comes up with a new library to be added to all the site that are created ?.

Of course you can create a new template after adding the new library, then what happens to all the sites that are already created ?, you may go to each and every site and create the library, what if there are hundreds of sites created ? , will it be practical to go to each site and add the library the answer is No because nobody is ready to do a task like this, You may of course use powershell script or a simple console application to do this but even that may not be possible in production environments.

So lets go to the solution, that is creating a web template and using several features to deploy the content and structure by doing this you can simply update the sites that are created and the site are to be created in the future.

This blog post series will have three more posts
1. Updating lists, adding lists
2. Updating webparts
3. Updating Webpart pages

The concept behind this is Feature Updating and all this is done using sandboxed solutions because it will work in Sharepoint Online as well.
I'm going to create 3 different features and show how to update them, when you create the site you can simply activate them and create a template from it and by upgrading the features you can simply update the sites.

Only problem you may face here is activating a new feature to existing sites, that of course will have to be done using the powershell.

Hope you find this post useful.
-Guruparan-

Tuesday, February 5, 2013

Download files from Sharepoint Library Recursively

Hi
This is a small program that will download the contents of a Sharepoint documnt library.

This will download all the files in the subfolders as well

class Program
{
 static void Main(string[] args)
 {
  Console.WriteLine("This Application will download the documents in a library");
  Console.Write("Enter Site URL : ");
  string SiteURL = Console.ReadLine();

  Console.Write("Library Name : ");
  string LibraryName = Console.ReadLine();

  //Open the site using the given url
  using (SPSite site = new SPSite(SiteURL))
  {
   //Open the web application
   using (SPWeb web = site.OpenWeb())
   {
    try
    {
     //Get the library folder
     SPFolder folder = web.GetFolder(LibraryName);

     //Download files
     downloadFilesrecursively(folder);
    }
    catch (Exception ex)
    {
     //Print the details of the exception
     Console.WriteLine("An Error Occured while processing " + ex.Message);
    }
   }
  }

  Console.ReadLine();
 }

 //The method that downloads the files from a given SPFolder
 public static void downloadFiles(SPFolder folder)
 {
  foreach (SPFile item in folder.Files)
  {
   //Download the file
   byte[] fileData = item.OpenBinary();
   System.IO.FileStream fstream = System.IO.File.Create(item.Name);
   fstream.Write(fileData, 0, fileData.Length);
   Console.WriteLine(item.Name + " " + fileData.Length);
  }
 }

 //Method that is used to call the download method recursively
 public static void downloadFilesrecursively(SPFolder folder)
 {
  //If the folder have subfolders call the download
  //Methods for the subfolders
  if (folder.SubFolders.Count != 0)
  {
   //Download the files in the current folder
   downloadFiles(folder);
   foreach (SPFolder item in folder.SubFolders)
   {
    downloadFilesrecursively(item);
   }
  }
  else
  {
   //If the folder has no subfolders
   //then download the file in the folder
     downloadFiles(folder);   
  }
 }
}