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
   }
}

Set up SharePoint Online Management Shell

Steps to setup "SharePoint Online Management Shell"

1. Install anyone from the below files based on your OS setup (32-bit or 64-bit)
a) Windows6.0-KB2506146-x64.msu
b) Windows6.0-KB2506146-x86.msu
c) Windows6.1-KB2506143-x64.msu (Worked in my case)
d) Windows6.1-KB2506143-x86.msu

2. Install 'SharePoint Online Management Shell' based on your OS (32-bit or 64-bit)
a) sharepointonlinemanagementshell_6008-1200_x64_en-us.msi (Worked in my case)
b) sharepointonlinemanagementshell_6008-1200_x86_en-us.msi

3. Install SharePoint Online Client Components SDK based on your OS (32-bit or 64-bit)
https://www.microsoft.com/en-us/download/confirmation.aspx?id=35585
a) sharepointclientcomponents_16-4002-1211_x64-en-us.msi (Worked in my case)
b) sharepointclientcomponents_16-4002-1211_x86-en-us.msi

4. Go to Start >> All Programs >> SharePoint Online Management Shell >> Right click and select ‘Run as administrator’.

5. Connect to Office 365 Admin site first using the below script. It should be run before running any script.
Connect-SPOService -Url <Admin site URL> -credential <Office 365 email address>
The email address should have access to Office 365 Admin Center for running PowerShell Online Script.
<Admin Site URL> say https://testlsharepoint-admin.sharepoint.com
<Office 365 email address> say kunnu@TestlSharePoint.onmicrosoft.com




Tuesday, January 17, 2017

Cascading of Lookup Dropdown Fields on SharePoint 2013 and Office 365

Welcome to an article on the ‘Cascading of Dropdown fields on SharePoint 2013 & Office 365’. This is a major approach when we build forms and when we have to use multiple look up fields on the form and they should be related to each other as a parent and child relationship.

So what happens is, when you select a parent, other child items are related to it. So if you select a particular parent, all the child items related to the parent will appear.

Let’s see it.


  • Create a list named “Company”.


  • Click on Add an app,
     Add an app

  • Choose the Custom List App.
  Custom List App

  • Click on it and create a ‘Company’ List.
create a Company List


  • Once the list is created, add in the names of the companies.

    list is created
  • As per the screenshot below.Company  

  • Create another list named as “Employee

    Create another list
  • Once the list is created, Create a look up column named as Company from our Company List.

    Company List
  • As you can see below, Clock on Create Column and the choose Lookup.

    Create Column
  • Select the list as Company and choose the Title field while configuring the lookup column.

    configuring the lookup
  • So here we can see our look up column has values from another list.

    values from another list
  • Fill in your required values; as for mine, I have filled in the name of the employees under the Title and have selected the company names from my look up field.
employees  
  • Now create a third list as Database.

    list as Database

    new item
  • Add the two look up columns, Company from the company list, and select title as the source of the column as per the screen shot below.

    Add the two look up column
  • Add the second look up columns; Employee from the employee list as per the screen shot below, selecting title as the field of source column.

    Employee from
  • So this will be your blank list view with all the columns,

    list view
  • When you click on New Item as you have added both the column as lookup they should display all the values but no, we are controlling their values as parent and child relationship as per the screen below.

    - If you select Company as “CTS”, the employee sees Manpreet as the value.
  • select Company as CTS

    CTS

    - Here when you select “Infy” as the Company name, Baghel is displayed as the Employee.

    Infy
  • How was it done?

    - Go to the Database list.

    - Under the list tab select “Default New Form”.

    Default New Form

    - Add a ‘Script Editor’ web part and paste the code below.

    database

    Code:
    1. <script src="https://site name/jquery.min.js"></script>  
    2. <script src="https://sitename/jquery.SPServices.min.js"></script>  
    3. <script type="text/javascript">  
    4. $(document).ready(function ()  
    5. {  
    6.     $().SPServices.SPCascadeDropdowns(  
    7.     {  
    8.         relationshipList: "Employee",  
    9.         relationshipListParentColumn: "Company",  
    10.         relationshipListChildColumn: "Title",  
    11.         parentColumn: "Company",  
    12.         childColumn: "Employee",  
    13.         debug: true  
    14.     });  
    15. });  
    16. </script>  
  • Click on OK and come back to the Database List and click on New Form.
  • Your Cascading will start functioning.
Here we saw today an amazing functionality of Cascading of lookup Dropdown fields on SharePoint 2013 & Office 365. Keep reading more articles and keep learning.