Friday, December 22, 2017

Customize the Printable view of a SharePoint list form

Hi Folks,

I got a task to customize the print view of 'DispForm.aspx'. I've utilized '@print media' query to achieve the functionality.I created a custom CSS file to add the '@print media' query.

The CSS file look as below.
#logo img{
display:none;
 }
 h2
 {
font-weight:bold;
 }
h3
{
font-weight:bold;
}
#tabHead
{
display:none;
}
#tabFooter img{
display:none;
}

@media print 
{
 nobr
 {
  font-weight:bolder;
  font-size:22px;
  font-family:Arial;
 }
 .ms-formbody
 {
font-size:22px;
font-family:Arial;
 }
 #logo img
 {
display:block;
height:80px;
direction:ltr;
top:auto;
 }
 .ms-standardheader
 { 
background-position:center;
text-align:left;
 }
 #tabPos > tbody:nth-child(2)
 {
padding-right:15px;
`}
 #tabPos
 {
text-align:left;
border:2;
width:80%;
 }
 #WebPartWPQ2 > table > tbody > tr:nth-child(3) > td
 {
 }
 .hideField
 {
display:none;
 }
 #WebPartWPQ2 > table > tbody > tr:nth-child(4) > td > table
 {
display:none;
 }
 #tabHead
 {
display:table-cell;
font-size:24px;
font-family:Arial;
 }
 #tabFooter img
 {
display:block;
 }
}

The print view would be displayed as shown below.












It helped me a lot and we can use it for future purpose.

Thanks,

JS Link use to display image hyperlink to List view column

Hi All,

I had a task to display an image hyperlink for the list view column. By clicking the image it will navigate to a page which is styled in print view of page.

At first, I added a single line of text field - PrintForm. Then I create a custom standard view- Print View. 'PrintForm' will be available in 'PrintView.aspx'.
I edit the page at first and then edit the web part which contains the web part.
I wrote a small JS code to achieve the functionality.

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {

SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
  Templates: {
Fields: {
           'PrintForm': {'View':function(ctx) {         
           return String.format("<a href='<CustomizedDisplayForm>.aspx?ID={0}' target='_blank'><img src='<ImageUrl>' width='42' height='42'></a>",  ctx.CurrentItem.ID);
}}

        }//Fields
  }//Templates
});
})


Once I completed the JS code, I appended the path of .js file to JS link in Miscellaneous section. After I saved the changes, the view would like as shown below.






Thanks for reading my post, I'll write another for print section.

Monday, September 18, 2017

PowerShell Error :- The local farm is not accessible.

Hi All,

I got a task to read all the users from SharePoint 2010 site. I used PowerShell Script to load the SharePoint DLLs and PowerShell script to read all the user profile information.

But, unfortunately the I got the below error.

I did some research and got to know that I don't have access to Shell Admin. It needs to be done explicitly. You can do it by yourself if you have Farm admin access.
SharePoint creates a shell admin security role in the database which is configurable through powershell
that's all that there is to it, you only need security admin to assign it
you never need database owner for any SharePoint work.

I used the below PowerShell script to add myself to Shell Admin.
$db = Get-SPDatabase | Where {$_.Name -eq “SharePoint_ConfigDB”}
Add-SPShellAdmin “domain\user_to_add” -database $db  

After the script got executed, I could able to run the PowerShell script to load user profile information.

The below link can help you with more information.


Thanks,
Kunal


Saturday, May 13, 2017

Running PowerShell Scripts in MOSS 2007 Server

Hi Folks,

I worked on a Proof of Concept to read all the user's usernames and their email addresses from a MOSS 2007 site. I've gone through many blogs where it mentioned to install Microsoft PowerShell 1.0. But believe me, it can work even with 'Windows PowerShell ISE' available in Start >> All Programs >> Accessories >> Windows PowerShell >> Windows PowerShell ISE. You need to run the scripts in the MOSS 2007 Server where your MOSS 2007 site is hosted.

I prefer you to read the below blog to set up PowerShell in MOSS 2007 Server.

https://nickgrattan.wordpress.com/2007/09/03/preparing-powershell-for-sharepoint-and-moss-2007/

You need to load file Microsoft.PowerShell_profile.ps1 before running the below script UserProfiles.ps1

[string]$FileName = ".\UsersList.csv"
$FileHeader = '"UserName", "Email"'
Add-Content -path $FileName -value $FileHeader
$site = New-Object Microsoft.SharePoint.SPSite("http://localhost/sites/intranet")
$groups = $site.RootWeb.sitegroups
foreach ($grp in $groups) {
foreach ($user in $grp.users) {
$UserEntry = '"'+ $user.name +'" ,+ "'+ $user.Email+'"'
Add-Content -path $FileName -value $UserEntry
} }
$site.Dispose()

This script also writes Usernames and Email Addresses to a CSV file.

The below mentioned link helped me to write the above script.
http://techtrainingnotes.blogspot.in/2010/12/sharepoint-powershell-script-to-list.html


Thanks,

Thursday, February 23, 2017

Countdown Web part

Add Add-ins for SharePoint

To add this add-in to your site, you'll need to have the latest version of SharePoint installed. Get the newest version of SharePoint.
1. Go to the SharePoint site where you want to install this add-in.
The URL might look like this: http://mycompany/myteam

2. Click the Settings icon at the top right corner of your site, and then click Add an add-in.
Settings

3. In the Find an add-in search box, paste the following tag and then click Search.
WA104379311

4. Click the text that tells you to check out the result in the SharePoint Store.

Link: https://store.office.com/help/addsharepointapps.aspx?ai=WA104379311

Tuesday, January 24, 2017

Powershell Online script to start multiple workflows

# Add Wave16 references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path (Resolve-Path "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll")
Add-Type -Path (Resolve-Path "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll")

# Specify tenant admin and site URL
$SiteUrl = "https://testlsharepoint.sharepoint.com/sites/developer/"
$ListName = "TestList"
$UserName = "kunnu@TestlSharePoint.onmicrosoft.com"
$SecurePassword = Read-Host -Prompt "Enter password" -AsSecureString

# Connect to site
$ClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$ClientContext.Credentials = $credentials
$ClientContext.ExecuteQuery()

# Get List and List Items
$List = $ClientContext.Web.Lists.GetByTitle($ListName)
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$ClientContext.Load($List)
$ClientContext.Load($ListItems)
$ClientContext.ExecuteQuery()

# Retrieve WorkflowService related objects
$WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($ClientContext, $ClientContext.Web)
$WorkflowSubscriptionService = $WorkflowServicesManager.GetWorkflowSubscriptionService()
$WorkflowInstanceService = $WorkflowServicesManager.GetWorkflowInstanceService()
$ClientContext.Load($WorkflowServicesManager)
$ClientContext.Load($WorkflowSubscriptionService)
$ClientContext.Load($WorkflowInstanceService)
$ClientContext.ExecuteQuery()
# Get WorkflowAssociations with List
$WorkflowAssociations = $WorkflowSubscriptionService.EnumerateSubscriptionsByList($List.Id)
$ClientContext.Load($WorkflowAssociations)
$ClientContext.ExecuteQuery()

# Prepare Start Workflow Payload
$Dict = New-Object 'System.Collections.Generic.Dictionary[System.String,System.Object]'

# Loop List Items to Start Workflow
For ($j=0; $j -lt $ListItems.Count; $j++){
    $msg = [string]::Format("Starting workflow {0}, on ListItemId {1}", $WorkflowAssociations[0].Name, $ListItems[$j].Id)
    Write-Host $msg
    #Start Workflow on List Item
    $Action = $WorkflowInstanceService.StartWorkflowOnListItem($WorkflowAssociations[0], $ListItems[$j].Id, $Dict)
    $ClientContext.ExecuteQuery()
}

PowerShell Online Script to terminate multiple workflows

Connect-SPOService -Url https://testlsharepoint-admin.sharepoint.com -credential kunnu@TestlSharePoint.onmicrosoft.com



# Add Wave16 references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll"

# Specify tenant admin and site URL
$SiteUrl = "https://testlsharepoint.sharepoint.com/sites/developer/"
$ListName = "TestList"
$UserName = "kunnu@TestlSharePoint.onmicrosoft.com"
$SecurePassword = Read-Host -Prompt "Enter password" -AsSecureString  

# Bind to site collection
$ClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$ClientContext.Credentials = $credentials
$ClientContext.ExecuteQuery()

# Get List
$List = $ClientContext.Web.Lists.GetByTitle($ListName)

$ClientContext.Load($List)
$ClientContext.ExecuteQuery()

$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$ClientContext.Load($ListItems)
$ClientContext.ExecuteQuery()

# Create WorkflowServicesManager instance
$WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($ClientContext, $ClientContext.Web)

# Connect to WorkflowSubscriptionService
$WorkflowSubscriptionService = $WorkflowServicesManager.GetWorkflowSubscriptionService()

# Connect WorkflowInstanceService instance
$WorkflowInstanceService = $WorkflowServicesManager.GetWorkflowInstanceService()

$ClientContext.Load($WorkflowServicesManager)
$ClientContext.Load($WorkflowSubscriptionService)
$ClientContext.Load($WorkflowInstanceService)
$ClientContext.ExecuteQuery()

# Get WorkflowAssociations with List
$WorkflowAssociations = $WorkflowSubscriptionService.EnumerateSubscriptionsByList($List.Id)
$ClientContext.Load($WorkflowAssociations)
$ClientContext.ExecuteQuery()

# Prepare Terminate Workflow Payload
$EmptyObject = New-Object System.Object
$Dict = New-Object 'System.Collections.Generic.Dictionary[System.String,System.Object]'

# Loop Terminate Workflow
For ($j=0; $j -lt $ListItems.Count; $j++){

   $msg = [string]::Format("Killing workflows {0} on ListItemID {1}", $WorkflowAssociations[0].Name, $ListItems[$j].Id)
   Write-Host $msg

   $itemWfInstances = $WorkflowInstanceService.EnumerateInstancesForListItem($List.Id, $ListItems[$j].Id)
   $ClientContext.Load($itemWfInstances)
   $ClientContext.ExecuteQuery()
   for ($k=0;$k -lt $itemWfInstances.Count;$k++)
   {
            try {
            $WorkflowInstanceService.TerminateWorkflow($itemWfInstances[$k])
                  $msg = "Worfklow terminated on " + $ListItems[$j].Id
                  $ClientContext.ExecuteQuery()
            } catch {
                  $msg = "Error terminating workflow on " + $ListItems[$j].Id + " Details: $_"
            }

            Write-Host $msg
   }
}