Showing posts with label SharePoint Online. Show all posts
Showing posts with label SharePoint Online. Show all posts

Friday, September 11, 2015

SharePoint Online: How To restore deleted items from Recycle bin with PowerShell.

If a SharePoint online user has deleted a huge amount of folders and files, he had in Document Library by chance, you can write a powershell script to restore it, instead do it manaully.

The script I have written can be fouund above on GitHub.


GitHub:

The script takes the following params:
  1.      Site collection administrator
  2.  User name who deleted files
  3.    Site collection url
  4.     Timestamp of items deleted date (ex., strart 2015-09-03 00:00:00 and end date 2015-09-03 23:59:59)



The script does the following actions:

  1. Takes  all site collection Recycle bin items and assign it into an array variable
  2.  It sorts an array in descending using merge method (https://gallery.technet.microsoft.com/scriptcenter/Merge-Sort-for-PowerShell-e5fdf3ab) (the first record in the last deleted)
  3. Limit deleted items count to the start date you enter in the script (ex. If recycle bin contains 8000 items from 2015-06-01 till 2015-09-09 and you want to restore items deleted for one particular day 2015-09-03, the items count will be limited to the start date)
  4.  Checks if deleted item is equal to timestamp
  5. Checks the user you entered in input
  6. Creates CSV report with sorted and deleted by the user items
  7.   Restores deleted items one by one from $csvReport filtered and sorted array with following methods

                                     $_.DeletedItem.Restore()
                                     $ctx.ExecuteQuery()
NOTE

      i you wish to get a report first to see what items will be restored first, just you need to comment the line with


                          $_.DeletedItem.Restore()
                           $ctx.ExecuteQuery()

and look at report.csv file in c:\tmp folder


The powershell script was created for a particular case, when the user deleted 25 GB of files stored on local drive via OneDrive and files were synhronized with SharePoint library. Files were deleted extremly fast and in one mass transaction. In this case, it was very important to get data in right order and hierarhy, as a Document libary has a lot of  nested folders and files . So you can takes the script as an example and modify it for your own case and a needs.

 Here a small example of Document library folder structure:





So have a nice day, J
I hope it will help somebody
Best regards,
Aleks

Monday, July 27, 2015

Show all user's schedule in calendar view by default without having to select people/group in selector (sharepoint 2013)

The Problem

One feature of SharePoint is to create a calendar and make it a group calendar. This allows the user to see the schedule of list of people at the same time on the same view as per the image below.
image

To configure the Calendar as a group Calendar you need to edit the List settings and then click on “List name, description and navigation” and finally select the radio button as per the image below.

image

So all this is SharePoint out of the box functionality. So where is the problem?

The problem is that to get the view of all calendar of your work group every time you open the calendar view you need to add each one of them individually! 

And there is no any way to persist this information so that it would open the calendar view automatically with the people added as per the list you see in the first image. 

The Solution

Here is a javascript workaround, which takes current site, get Calendar List ID, enumerates through List Roles and select groups contains "Member" word. You can find this place a replace the group name

1. Place script editor web part on page with calendar view
2. Copy and insert code snippet 


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" unselectable="on">
    //Change to name of your SharePoint group with the team members in it
var groupName = "";
//Change to the id of your SharePoint group with the team members in it
var groupID = "";

    $(document).ready(function () {
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', execOperation);
    });

    function execOperation() {
        var currentContext = new SP.ClientContext.get_current();
        this.oWeb = currentContext.get_web();
        currentUser = this.oWeb.get_currentUser();
        var allGroups = currentUser.get_groups(); 
        var oList = currentContext.get_web().get_lists().getById(_spPageContextInfo.pageListId)
        currentContext.load(oList);
        console.log(oList);
        var listRoleAssignments = oList.get_roleAssignments()
        currentContext.load(listRoleAssignments,'Include(Member,RoleDefinitionBindings)');
        currentContext.load(allGroups);
currentContext.executeQueryAsync(OnSuccess,OnFailure);

function OnSuccess(sender, args) {
    //enumerate results
    var listPermsEnumerator =  listRoleAssignments.getEnumerator();
    while (listPermsEnumerator.moveNext()) {
        var rAssignment = listPermsEnumerator.get_current();
        var member = rAssignment.get_member();
        var grName = member.get_title();
        if (grName.indexOf("Members") >= 0) {
            groupName = grName;
            groupID = member.get_id();
        
            break;
        }
    } 
}

function OnFailure(sender, args) {
}   
}
function _firstTime() {
    //need to select the calendar tab so we can override the onlick method on some of the buttons.
    SelectRibbonTab('Ribbon.Calendar.Calendar', true);
    //give the ribbon time to load
    setTimeout('_doWireUp();',1000);
}

function _doWireUp()
{
    //change the onclick event for the group buttons to make sure it reloads our default group
    var weekElem = document.getElementById('Ribbon.Calendar.Calendar.Scope.WeekGroup-Large');
    if(weekElem)
        weekElem.onclick = function() {setTimeout('_setDefaultResources();',1000);return false;};

    var dayElem = document.getElementById('Ribbon.Calendar.Calendar.Scope.DayGroup-Large');

    if(dayElem)
        dayElem.onclick = function() {setTimeout('_setDefaultResources();',1000);return false;};

    _setDefaultResources();
}


function _setDefaultResources() {
    //this is where the magic happens
    var el = jQuery('.ms-acal-rootdiv');
    var tmpstr = "\u003cEntities Append=\u0022True\u0022 Error=\u0022\u0022 DoEncodeErrorMessage=\u0022True\u0022 Separator=\u0022;\u0022 MaxHeight=\u00223\u0022\u003e\u003cEntity Key=\u0022MyGroup\u0022 DisplayText=\u0022MyGroup\u0022 IsResolved=\u0022True\u0022 Description=\u0022MyGroup\u0022\u003e\u003cExtraData\u003e\u003cArrayOfDictionaryEntry xmlns:xsi=\u0022http:\u002f\u002fwww.w3.org\u002f2001\u002fXMLSchema-instance\u0022 xmlns:xsd=\u0022http:\u002f\u002fwww.w3.org\u002f2001\u002fXMLSchema\u0022\u003e\u003cDictionaryEntry\u003e\u003cKey xsi:type=\u0022xsd:string\u0022\u003eSPGroupID\u003c\u002fKey\u003e\u003cValue xsi:type=\u0022xsd:string\u0022\u003eMyGroupID\u003c\u002fValue\u003e\u003c\u002fDictionaryEntry\u003e\u003cDictionaryEntry\u003e\u003cKey xsi:type=\u0022xsd:string\u0022\u003eAccountName\u003c\u002fKey\u003e\u003cValue xsi:type=\u0022xsd:string\u0022\u003eMyGroup\u003c\u002fValue\u003e\u003c\u002fDictionaryEntry\u003e\u003cDictionaryEntry\u003e\u003cKey xsi:type=\u0022xsd:string\u0022\u003ePrincipalType\u003c\u002fKey\u003e\u003cValue xsi:type=\u0022xsd:string\u0022\u003eSharePointGroup\u003c\u002fValue\u003e\u003c\u002fDictionaryEntry\u003e\u003c\u002fArrayOfDictionaryEntry\u003e\u003c\u002fExtraData\u003e\u003cMultipleMatches \u002f\u003e\u003c\u002fEntity\u003e\u003c\u002fEntities\u003e";
    var tmpstr2 = tmpstr.replace('MyGroupID',groupID);
    var xml = tmpstr2.replace(/MyGroup/g,groupName);
    var sel = SP.UI.ApplicationPages.CalendarSelector.instance().getSelector(1, $(el).attr('ctxid'));
    sel.selectEntities(xml, true);
}
ExecuteOrDelayUntilScriptLoaded(_firstTime, "sp.ribbon.js");

</script>


so enjoy. :)

Please comment if you have any question.

Have a good day :)


Tuesday, July 21, 2015

SharePoint Online site recycle bin report by user name and items count deleted by that user


Hi,

here you will find a Powershell Script created by me to scan SharePoint Online site Recycle Bin. The script runs throught the all deleted items, sorts received item collection by deleted date using Merge sort algorithm and return items count and user names which were deleted by the user in period your entered when you requested.

For sorting items I have used the powershell scripts created by James Keeler

The powershell script can be found here:


Have a good time, J

Good luck.