Saturday, September 24, 2016

Export Term Store to CSV

Hii,

Below is the script that has been executed to export terms into csv of Taxonomy

Add-PSSnapin microsoft.sharepoint.powershell
$siteUrl = "siteurl"
$outputDir = "E:\TermStore"

  function Get-TermSetsCSV() {
     param($SiteUrl, $CSVOutput)

      $empty = ""
    $taxonomySite = Get-SPSite -Identity $SiteUrl
     #Connect to Term Store in the Managed Metadata Service Application
    $taxonomySession = Get-SPTaxonomySession -site $taxonomySite
  $taxonomyTermStore =  $taxonomySession.TermStores | Select Name
   $termStore = $taxonomySession.TermStores[$taxonomyTermStore.Name]
   foreach ($group in $termStore.Groups)
   {
       foreach($termSet in $group.TermSets)
         {
            $terms = @()
              #The path and file name, in this case I did C:\TermSet\TermSetName.csv
             $CSVFile = $CSVOutput + '\' + $termSet.Name + '.csv'

           #From TechNet: The first line of the file must contain 12 items separated by commas
             $firstLine = New-TermLine -TermSetName $termSet.Name -TermSetDescription $empty -LCID $empty -AvailableForTagging "TRUE" -TermDescription $empty -Level1 $empty -Level2 $empty -Level3 $empty -Level4 $empty -Level5 $empty -Level6 $empty -Level7 $empty
            $terms+=$firstLine
             #Now we start to add a line in the file for each term in the term set
             foreach ($term in $termSet.GetAllTerms())
            {
                 $tempTerm = $term
                $counter = 0
                 $tempTerms = @("","","","","","","")
               #this while loop makes sure you are using the root term then counts how many child terms there are
                while (!$tempTerm.IsRoot)
                 {
                    $tempTerm = $tempTerm.Parent
                     $counter = $counter + 1
                 }
                 $start = $counter

                #this makes sure that any columns that would need to be empty are empty
                #i.e. if the current term is 3 levels deep, then the 4th, 5th, and 6th level will be empty
                while ($counter -le 6)
                {
                     $tempTerms[$counter] = $empty
                       $counter = $counter + 1
              }

                  #start with the current term
                 $tempTerm = $term
                #fill in the parent terms of the current term (there should never be children of the current term--the child term will have its own line in the CSV)
             while ($start -ge 0)
                  {
                     $tempTerms[$start] = $tempTerm.Name
                      $tempTerm = $tempTerm.Parent
                      $start = $start - 1
               }

                #create a new line in the CSV file
                 $CSVLine = New-TermLine -TermSetName $empty -TermSetDescription $empty -LCID $empty -AvailableForTagging "TRUE" -TermDescription $empty -Level1 $tempTerms[0] -Level2 $tempTerms[1] -Level3 $tempTerms[2] -Level4 $tempTerms[3] -Level5 $tempTerms[4] -Level6 $tempTerms[5] -Level7 $tempTerms[6]
                 #add the new line
               $terms+=$CSVLine
             }

             #export all of the terms to a CSV file
            $terms | Export-Csv $CSVFile -notype
         }
   }
     $taxonomySite.dispose()
}
 #constructor
function New-TermLine() {
      param($TermSetName, $TermSetDescription, $LCID, $AvailableForTagging, $TermDescription, $Level1, $Level2, $Level3, $Level4, $Level5, $Level6, $Level7)
      $term = New-Object PSObject
     $term | Add-Member -Name "TermSetName" -MemberType NoteProperty -Value $TermSetName
     $term | Add-Member -Name "TermSetDescription" -MemberType NoteProperty -Value $TermSetDescription
    $term | Add-Member -Name "LCID" -MemberType NoteProperty -Value $LCID
     $term | Add-Member -Name "AvailableForTagging" -MemberType NoteProperty -Value $AvailableForTagging
     $term | Add-Member -Name "TermDescription" -MemberType NoteProperty -Value $TermDescription
     $term | Add-Member -Name "Level1" -MemberType NoteProperty -Value $Level1
       $term | Add-Member -Name "Level2" -MemberType NoteProperty -Value $Level2
      $term | Add-Member -Name "Level3" -MemberType NoteProperty -Value $Level3
    $term | Add-Member -Name "Level4" -MemberType NoteProperty -Value $Level4
    $term | Add-Member -Name "Level5" -MemberType NoteProperty -Value $Level5
     $term | Add-Member -Name "Level6" -MemberType NoteProperty -Value $Level6
     $term | Add-Member -Name "Level7" -MemberType NoteProperty -Value $Level7
     return $term
  }
 Get-TermSetsCSV -SiteUrl $siteUrl -CSVOutput $outputDir

Change Page Layout using PowerShell

Hi,

We had a requirement of changing page layout through PowerShell i.e.

If certain pages are using page layout pagelayout1,need to change those pagelayout2

So through PowerShell script, I am checking if the page is utilizing pagelayout1,

If it is, then I am changing to page layout2 of that page as below

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Variables for Web and Page URLs
$WebURL="siteurl"

#Get the web and page
$Web = Get-SPWeb $WebURL
#Get Publishing Site and Web
$PublishingSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($Web.Site)
$PublishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)

#Get Pages Library
$PublishingPages = $PublishingWeb.GetPublishingPages()
#Iterate through each page
foreach ($Page in $PublishingPages)
{
    $pageurl = $Page.url
    #Get the web and page
    $Web = Get-SPWeb $WebURL
    $File = $Web.GetFile($pageurl)

    if($File.Properties["PublishingPageLayout"].Contains("_catalogs/masterpage/pagelayout1.aspx"))
    {
    write-host "Got it"
    $File.CheckOut("Online",$null)
    $File.Properties["PublishingPageLayout"] = $WebURL+"_catalogs/masterpage/pagelayout2.aspx, Verified"
    $File.Update()
    $File.CheckIn("Page layout updated via PowerShell",[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
    }    
    
}
$Web.Dispose()

Saturday, August 20, 2016

Displaying List View Web Part within selected date range using jQuery/j JavaScript

Hi,

We had a requirement of showing list values based upon selected date range.

Before writing a script. have created Calculated Column as ‘CustomCreatedDate’ using ‘Created’ column

I should thank below author which helped me in achieving this.


<script>
//jQuuery datepicker
$(function() {
               
               
                decodedUrl = decodeURIComponent(window.location.href);
    $( "#startdatepicker" ).datepicker({
                                changeMonth: true,
                                changeYear: true,
                                maxDate: 0
                });
    $( "#enddatepicker" ).datepicker({
                                changeMonth: true,
                                changeYear: true,
                                maxDate: 0
                });
               
     if(GetParameterValues('FilterValue1') && GetParameterValues('FilterValue2')){
                                if((GetParameterValues('FilterValue1')!='undefined' && GetParameterValues('FilterValue2')!='undefined')|| (GetParameterValues('FilterValue1')!='' && GetParameterValues('FilterValue2')!='')){
                                                var fromdate=new Date(GetParameterValues('FilterValue1'));
                                                 document.getElementById('startdatepicker').value=fromdate.format("MM/dd/yyyy");
                                                var todate =new Date(GetParameterValues('FilterValue2'));
                                                document.getElementById('enddatepicker').value=todate.format("MM/dd/yyyy");
                                                $('#empty-WPQ2').closest('div').prev('table').hide();
                                                $('#empty-WPQ2 td').text("There are no records to show");
                                                $('.filterdList').hide();                                   
                                }             
                 }
                else {
                                $('.ms-listviewtable').hide();
                                $('.ms-promlink-button-right').hide();
                                $('.ms-paging').hide();
                                $('.filterdList').hide();
                                $('.ms-promlink-button').hide();
                }
                //Rename 'Created' column to 'Accepted Date'
                $("div[displayname='Created'] a").text("Accepted Date");
                //Hide 'Select All' tick mark
                $('.ms-selectall-span').hide();
                //Hide 'Select' tick mark
                $('.ms-selectitem-span').hide();
               
                 //Remove Filter
                $(".ms-listviewtable th").each(function(){                          
                                $(this).attr("onmouseover","");
                                $(this).attr("onmousedown","");                            
                });
                $(".ms-listviewtable .ms-headerSortTitleLink").each(function(){
                                $(this).parent().html("<span>"+$(this).text()+"</span>");                         
                });
               
                $(".s4-wpcell").removeAttr('onkeyup').removeAttr('onmouseup');
               
               
});

function GetParameterValues(param) { 
            var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
                                                //var decodedUrl = decodeURIComponent(url);
            for (var i = 0; i < url.length; i++) { 
                var urlparam = url[i].split('='); 
                if (urlparam[0] == param) { 
                    return urlparam[1]; 
                } 
            } 
        } 

/*Entry point to our script*/
function getFilter()
{
    var sourceURL =document.URL; //get the URL
                /*
    Remove existing querystrings added by our script and gives us a clean URL.
    This removes the parameters FilterField1,FilterOp1,FilterValue1,FilterField2,FilterOp2,FilterValue2
    */
    var url = removeParam(sourceURL);
    var from = document.getElementById('startdatepicker').value;
    var end= document.getElementById('enddatepicker').value;
    if(from!='' && from !='undefined')
    {
        if(end!='' && end!='undefined')
        {
            if(from <= end)
            {
            var startDate = new Date(from)
            var toDate = new Date(end);
            var startdate=startDate.format("yyyy/MM/dd");//convert format yyyy/MM/dd. Here we are using the format prototype.
            var enddate=toDate.format("yyyy/MM/dd");
            window.location = url+'FilterField1=Created&FilterValue1='+startdate+'&FilterOp1=Geq&FilterField2=CustomCreatedDate&FilterValue2='+enddate+'&FilterOp2=Leq';
                                                                               
                                               
            }
            else
            {
            alert("Please ensure that the To Date is greater than or equal to the From Date.");
            return false;
            }
        }                     
        else
        {
            alert("Please select To date");
            return false;
        }                     
                               
    }
    else
    {
        alert("Please select From date");
        return false;
    }         
}

function removeParam(sourceURL) {
    var rtn = sourceURL.split("?")[0],
        param,
        params_arr = [],
        queryString = (sourceURL.indexOf("?") !== -1) ? sourceURL.split("?")[1] : "";
    if (queryString !== "") {
        params_arr = queryString.split("&");
        for (var i = params_arr.length - 1; i >= 0; i -= 1) {
            param = params_arr[i].split("=")[0];
            if (param === "FilterField1") {
                params_arr.splice(i, 1);
            }
            if (param === "FilterValue1") {
                params_arr.splice(i, 1);
            }
            if (param === "FilterOp1") {
                params_arr.splice(i, 1);
            }
            if (param === "FilterField2") {
                params_arr.splice(i, 1);
            }
            if (param === "FilterValue2") {
                params_arr.splice(i, 1);
            }
            if (param === "FilterOp2") {
                params_arr.splice(i, 1);
            }
        }
        rtn = rtn + "?" + params_arr.join("&");
    }
    else
    {
        rtn=rtn+"?";
    }
    return rtn;
}
</script>
<h1 class="searchHeading">Search for Lady Employees who Accepted Guidelines</h3>

<div class="timePeriod">
                <div class="add-lbl-wrp">
                                <label class="add-label">Filter time period</label>
                </div>
                <div id="Main_Left_divStartDate" class="float-l from">
                                <div class="add-lbl-wrp">
                                                <span id="Main_Left_lblStartDate" class="add-label">From Date</span>
                                                <input type="hidden" id="Main_Left_hdnStartDate" value="Start Date">
                                </div>

                                <input type="text" id="startdatepicker" class="txt-bx" placeholder="Mandatory" readonly='true'/>
                               
                </div>
                <div id="Main_Left_divEndDate" class="float-l last">
                                <div class="add-lbl-wrp">
                                                <span id="Main_Left_lblEndDate" class="add-label">To Date</span>
                                                <input type="hidden" id="Main_Left_hdnEndDate" value="End Date">
                                </div>
                               
                                <input type="text" id="enddatepicker" class="txt-bx" placeholder="Mandatory" readonly='true'/>
                               
                </div>
                <input id="btnFilter" onclick="getFilter()" type="button" value="Search"  class="float-r"/>
</div>
<h3 class="filterdList">List of Lady Minds</h3>
<style type="text/css">
.article .related-links,.article .article-header{display:none;}
h1.searchHeading,.filterdList{font:16px open_sanssemibold;margin:0 0 20px;color:#333;}
.filterdList{margin:0 0 10px;}
.timePeriod {width: 600px;padding: 2px 10px 10px;background: #f2f2f2;border: 1px solid #ccc;display: inline-block;min-height: 96px;margin:0 0 20px}
.timePeriod .add-lbl-wrp {line-height: 17px;margin: 0 0 2px;}
.timePeriod .add-label {margin: 0;padding: 1px 10px 1px 3px;text-transform: uppercase;line-height: 11px;}
.timePeriod .from {margin: 0 20px 0 0;}
.timePeriod .float-l {position: relative;margin-top: 20px;width: 286px;}
input[type=text].txt-bx, .textarea {border: none;border-bottom: 1px dashed #999 !important;max-width: 300px;padding: 0;font: 15px open_sansregular, helvetica, arial;min-height: 28px;}
.timePeriod .float-l .txt-bx {width: 286px;background: url('/transport/PublishingImages/calendor_icon.png') no-repeat scroll right 5px transparent;cursor:pointer;}
#btnFilter{background: #d0d0d0;font: 10px/20px open_sansbold,helvetica,arial;text-decoration: none;color: #444;border: 0;margin: 10px 10px 0 20px;display: inline-block;cursor: pointer;padding:0;}
.txt-bx::-webkit-input-placeholder{font-family: open_sansitalic, helvetica, arial;color: #999999;}
.txt-bx:-moz-placeholder{/* Firefox 18- */font-family: open_sansitalic, helvetica, arial;color: #999999;}
.txt-bx::-moz-placeholder{          /* Firefox 19+ */font-family: open_sansitalic, helvetica, arial;color: #999999;}
.txt-bx:-ms-input-placeholder{font-family: open_sansitalic, helvetica, arial;color: #999999;}
[summary="TransportLadyEmployees"]{width:100%;}
[summary="TransportLadyEmployees"] tr:nth-child(even){background:#f7f7f7;}
#DeltaPlaceHolderMain [summary="TransportLadyEmployees"] tr td{vertical-align:middle;}
[summary="TransportLadyEmployees"] tr:nth-child(odd).ms-viewheadertr{background:#fff;}
[summary="TransportLadyEmployees"] tr:nth-child(odd).ms-viewheadertr th{border-bottom:1px solid #d7d7d7;padding-bottom:5px;}
[summary="TransportLadyEmployees"] tr:nth-child(odd).ms-viewheadertr th a{color:#000!important;font:13px open_sanssemibold;}
.ms-bottompaging {margin: 10px 22px 0 0;}
div#WebPartWPQ2 {width: 624px;height: 360px;overflow-y: auto;}
td.ms-vb-itmcbx.ms-vb-imgFirstCell >div {display: none;}
div.article, div.welcome{padding:0px;}
.filterdList {margin:0px;}
.ms-webpart-cell-vertical {display: inherit;}
#empty-WPQ2 td{padding:20px;text-align:center;font:13px open_sanssemiboldItalic;}

</style>

After this I was able to display the list of records as below