Tuesday, February 21, 2017

Get folder and file names from SharePoint SiteCollection using PowerShell

Get folder and file names from SharePoint SiteCollection using PowerShell
Step1: Copy below code into notepad and save as 'getFiles.ps1' and save.
--------------------------------------
Start-SPAssignment -Global | Out-Null
function Get-SPWebs($SiteCollection)
{
       $SiteCollection = Get-SPSite $SiteCollection
       $webs = @()
       $SiteCollection.allwebs | %{$webs += $_.url}
       return $webs
}
function Get-SPFolders($webs)
{
       foreach($web in $webs)
       {
              $web = Get-SPWeb $web
              Write-Host "`n$($web.url)" -ForegroundColor Green
              Add-Content C:\log01.txt $($web.url)
              $lists = $web.lists | ?{$_.Hidden -eq $false}
              #$lists = $web.lists | ?{$_.itemcount -ge "1" -And $_.Hidden -eq $false -And $_.BaseType -eq "DocumentLibrary"}
              #$lists = $web.lists | ?{$_.title -eq "Documents" -and $_.itemcount -ge "1" -And $_.BaseType -eq "DocumentLibrary"} #Change any identifier here
              foreach($list in $lists)
              {                   
                     Write-Host "- $($list.RootFolder.url)"
                  Add-Content C:\log01.txt "  - $($list.RootFolder.url)"
                     $rootfolder = $web.GetFolder($list.RootFolder.Url)
                     Get-SPFileNames($rootfolder)
                     foreach($folder in $list.folders)
                     {
                           $folder = $web.GetFolder($folder.url)
                           Get-SPFileNames($folder)
                     }
              }
              $web.dispose()
       }
}
function Get-SPFileNames($folder)
{
       foreach($file in $folder.Files)
       {            
              Add-Content C:\log01.txt "        - $($file.Url)"
       }
}
$Sitecollection = "http://SharePointURL:1234"
$webs = Get-SPWebs -SiteCollection $Sitecollection
Get-SPFolders -Webs $webs
Stop-SPAssignment -Global
--------------------------------------
Step2: Open 'SharePoint Management Shell' as 'Run as administrator' -> open 'getFiles.ps1' and click enter to execute script.
Step3: OutPut: we can similar out as below.
--------------------------------------
http://SharePointSiteURL:1234
   - DocLib1
        - DocLib1/Doc2.txt
        - DocLib1/Doc1.rtf
        - DocLib1/folder1/CustomList1.JPG
        - DocLib1/folder2/Bootstrap.JPG
  - Shared Documents
        - Shared Documents/ImageSlider/4.jpg
        - Shared Documents/ImageSlider/7.jpg
  - SitePages
        - SitePages/ImageSlider.aspx
  - Style Library
        - Style Library/Media Player/MediaWebPartPreview.png
  - Lists/test
        - Lists/test/DispForm.aspx
        - Lists/test/EditForm.aspx
        - Lists/test/AllItems.aspx
        - Lists/test/view1.aspx
        - Lists/test/NewForm.aspx
-------------------------------------- 

Friday, December 30, 2016

Display SharePoint List Data in JQuery Data Table using JQueryUI and Bootstrap 3

Display SharePoint List Data in JQuery Data Table using JQueryUI and Bootstrap 3

Step 1: 
-------
First Create an Custom List and add sample data as shown below.

Step 2: 
-------
Open an Notepad file a and add below script and save as '.html' file and upload into SharePoint Site Assets Library. (Update your list name in below script).

1.Display SharePoint List Data in JQuery Data Table using JQueryUI
---------------------------------------------------------------------
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title></title>
    <script type="text/javascript" src="//code.jquery.com/jquery-1.12.4.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>   
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.13/js/dataTables.jqueryui.min.js"></script>  
    <link rel="stylesheet" type="text/css" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">       
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.13/css/dataTables.jqueryui.min.css">
    <script type="text/javascript">
        $(document).ready(function () {
            var RestUrl = "../_vti_bin/listdata.svc/CustomList1";
            $.ajax({
                url: RestUrl,
                method: "GET",
                headers: {
                    "accept": "application/json;odata=verbose",
                },
                success: function (data) {
                    if (data.d.results.length > 0) {
                        $('#divCustomList1').append(getJSONData(data.d.results));

                        var oTable = $('#tblCustomList').DataTable({
                        });

                    } else {
                        $('#divCustomList1').append("<span>No List Data Found.</span>");
                    }
                },
                error: function (data) {
                    $('#divCustomList1').append("<span>Error Retreiving List Data. Error : " + JSON.stringify(data) + "</span>");
                }
            });
            function getJSONData(objArray) {
                var tableContent = '<table id="tblCustomList" class="display" cellspacing="0" width="100%">'
                + '<thead>'
                   + '<tr>'
                + '<th>ID</th>'
                           + '<th>Title</th>'
                           + '</tr>'
                            + '</thead>'
              + '<tbody>';
                for (var i = 0; i < objArray.length; i++) {
                    tableContent += '<tr>';
                    tableContent += '<td>' + objArray[i].Id + '</td>';
                    tableContent += '<td>' + objArray[i].Title + '</td>';
                    tableContent += '</tr>';
                }
                +'</tbody>';
                return tableContent;
            }
        });
</script>
</head>
<body>
<div id="div1">
    <table style="width: 100%;">
        <tr>
            <td>
                <div id="divCustomList1" style="width: 100%"></div>
            </td>
        </tr>
    </table>
</div>
</body>
</html>

2.Display SharePoint List Data In JQuery Data Table using Bootstrap 3
------------------------------------------------------------------------
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title></title>
    <script type="text/javascript" src="//code.jquery.com/jquery-1.12.4.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.13/js/dataTables.bootstrap4.min.js"></script>      
    <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.5/css/bootstrap.css">
       <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.13/css/dataTables.bootstrap4.min.css">     
    <script type="text/javascript">
        $(document).ready(function () {
            var RestUrl = "../_vti_bin/listdata.svc/CustomList1";
            $.ajax({
                url: RestUrl,
                method: "GET",
                headers: {
                    "accept": "application/json;odata=verbose",
                },
                success: function (data) {
                    if (data.d.results.length > 0) {
                        $('#divCustomList1').append(getJSONData(data.d.results));

                        var oTable = $('#tblCustomList').DataTable({
                        });

                    } else {
                        $('#divCustomList1').append("<span>No List Data Found.</span>");
                    }
                },
                error: function (data) {
                    $('#divCustomList1').append("<span>Error Retreiving List Data. Error : " + JSON.stringify(data) + "</span>");
                }
            });
            function getJSONData(objArray) {
                var tableContent = '<table id="tblCustomList" class="table table-striped table-bordered" cellspacing="0" width="100%" cellspacing="0" width="100%">'
                + '<thead>'
                   + '<tr>'
                + '<th>ID</th>'
                           + '<th>Title</th>'
                            + '</tr>'
                            + '</thead>'
              + '<tbody>';
                for (var i = 0; i < objArray.length; i++) {
                    tableContent += '<tr>';
                    tableContent += '<td>' + objArray[i].Id + '</td>';
                    tableContent += '<td>' + objArray[i].Title + '</td>';
                    tableContent += '</tr>';
                }
                +'</tbody>';
                return tableContent;
            }
        });
</script>
</head>
<body>
<div id="div1">
    <table style="width: 100%;">
        <tr>
            <td>
                <div id="divCustomList1" style="width: 100%"></div>
            </td>
        </tr>
    </table>
</div>
</body>
</html>

Step 3: 
-------
Add an Content Editor WebPart to page and give script reference from Site Assets Library.

DataTable Using JQueryUI:
--------------------------

DataTable using Bootstrap:
--------------------------