Wednesday, October 21, 2015

List SharePoint subsites on SharePoint page using webpart

If you would like to rollup the subsites under a SharePoint site use the below script and paste on a script editor webpart on any page.

This rollup using REST query also looks at the permission and displays site that the current logged in user has access to. You can change the URL to point to any subsite where you want to roll up the sites from.

<div id="resortslist"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>

$(document).ready(function () {

    GetSubSites();

});

    // function to check contract form status
    function GetSubSites()
    {

        $.ajax(
        {
            url: _spPageContextInfo.webServerRelativeUrl +
            "/_api/web/webs/?$select=Title,Created,Description,LastItemModifiedDate,Url&$filter=effectivebasepermissions/high%20gt%2032",
            type: "GET",
            headers: {
                "accept": "application/json;odata=verbose",
            },
            success: function (data)
            {
                readContractFormSuccess(data);
            },
            error: function (err)
            {
                alert(JSON.stringify(err));
            }
        }
        );
    }

    // success function for contract form check
    readContractFormSuccess = function (data)
    {
     
        var results = data.d.results;


        if (results.length > 0) {


            var html = "<div id='resorts-site-elements'><br/>";
            html += "<table>";
            html += "<tr><td  style='padding-right:10px>Resort Name</td></tr>"
            for (var i = 0; i < results.length; i++)
            {
                           
                     

                var created = results[i].Created;
                var descp =results[i].Description;
             
                var sitetitle =  results[i].Title;
                var link =results[i].Url;
                       


                html += "<tr><td style='padding-right:10px'>"
                html += "<a href='"+ link + "'>" + sitetitle + "</a>";
                html += "</td> ";
                 
                html += "</tr>";
            }
        }

        else {
            html += "<tr></td>No Sites</td></tr>";
        }

        html += "</table>";
        html += "</div>";
        $("div[id='resortslist']").html(html).show();
     
    }

 
</script>

Kickoff SharePoint 2013 Worklfows using PowerShell

Here is the script I created and used for triggering SP 2013 Worklfows. This does not work for 2010 workflows. You can schedule this script on Task Scheduler on any Machine installed with SharePoint bits

cls
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

$sourceWebURL = 'http://xxx.com/sites/xxx/subsite'
$sourceListName = 'TaskNotifications'
$TargetWorkflow = 'NotifyPendingTasks'
$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]
$items = $spSourceList.getItems()

#-- Getting a Workflow manager object to work with.
$wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($spSourceweb)
#-- Getting the subscriptions
$sub = $wfm.GetWorkflowSubscriptionService()
#-- Getting the specific workflow within the list of subscriptions on the specific list. (SP2010 associated workflows basically)
$WF = $sub.EnumerateSubscriptionsByList($spSourcelist.ID) | Where-Object {$_.Name -eq "$TargetWorkflow"}
#-- Getting a Workflow instance in order to perform my commands.
$wfis=$wfm.GetWorkflowInstanceService()


Foreach($item in $items){
 
    $object = New-Object 'system.collections.generic.dictionary[string,object]'
    $object.Add("WorkflowStart", "StartWorkflow");
   $wfis.StartWorkflowOnListItem($WF, $item.ID, $object)
}