September 13, 2016

Enable Auditing for SharePoint Online & SharePoint 2013 On-Premise

Auditing in SharePoint Online and SharePoint 2013 On-Premise
SharePoint Online/On-Premise has built-in audit feature that enable us to track administrative activities.

With SharePoint auditing, we can monitor site collection level administration such as permission changes, role updates, group membership changes and changes to the audit policy itself.

There are few areas of SharePoint administration which can't be audited, such as; farm administrator assignments, site collection creation and other farm level operations.

For SharePoint auditing, we have to use “Audit log reports” settings from Site Collection Settings. Both, SharePoint Online (Office 365) and SharePoint On-Premise have different options for auditing settings.

Steps to configure Auditing in SharePoint:
1. Navigate to Site Settings of Root level site.
2. Click "Audit log reports" under "Site Collection Administration" section.
3. Configure the Auditing according to the requirements.

1. Following image shows audit settings that are available in SharePoint Online (Office 365).

2. Following image shows audit settings that are available in SharePoint 2013 On-Premise.

FAQs
1. Can we increase duration of storage for SharePoint audit reports?
For SharePoint auditing, it provides report based logs that can be utilize to see overall all usage of sites, documents, lists, etc.

To see audit log, first we have to select location (SharePoint list) where we want to save our audit log report.

If we are using Office 365, then storage limit is based on our storage quota for Office 365 tenant. If we are using On-Premise version, then storage quota is based on storage capacity of our server.

Storage limit is also based on list, maximum items it can handle. We can also set document retain settings after which audit report will be deleted.

2.  Can we move and save SharePoint audit logs to another location?
Audit logs are saved to list as excel file and we can download that file to check the report. We can move this audit log files as we desire and this way we can save/move logs.

3.  Can we increase SharePoint audit log details?
We cannot change logging details for auditing, but we can use audit settings where it provides some items that can be included/removed from audit log report.

If you have any questions you can reach out our SharePoint Consulting team here.

September 8, 2016

Injecting Javascript file through SharePoint Hosted App

A requirement came across was to execute a JavaScript on all the pages of SharePoint Site. Here, option of achieving the functionality by developing custom master page was eliminated as we were dealing with a SharePoint Hosted App. It is also easy to achieve through server side but need to find solution on client side as SharePoint Hosted App has everything on client side. Finally, we found a solution to inject the JavaScript using Custom Action.

JavaScript Object Model (JSOM) allows us to programmatically register Custom Action with Host Web in a way similar to declarative approach used with Farm or Sandboxed Solution. In order to register a Custom Action using JSOM, your app requires the Full Control permission on the Site/Site Collection where you want to register it.

To achieve this, we need to perform two steps as below:
  1. Provisioning the JavaScript file from App web to Host web
  2. Registering the Custom action with the file path referring to that JavaScript file in the Host web.

1. Provisioning the JavaScript file from app web to host web:

function uploadFileToHostWebViaCSOM(serverRelativeUrl, filename, contents) {      
        var executor = new SP.RequestExecutor(window.appWebUrl);
        executor.executeAsync(
                {
                    url: window.appWebUrl + "/_api/SP.AppContextSite(@target)/web/GetFolderByServerRelativeUrl('" + serverRelativeUrl + "')/Files?@target='" + window.hostWebUrl + "'",
                    method: "GET",
                    headers: { "Accept": "application/json; odata=verbose" },
                    success: function (data) {
                        var jsonObject = JSON.parse(data.body);
                        var createInfo = new SP.FileCreationInformation();
                        createInfo.set_content(new SP.Base64EncodedByteArray());
                        for (var i = 0; i < contents.length; i++) {
                            createInfo.get_content().append(contents.charCodeAt(i));
                        }
                       createInfo.set_url(filename);
                       createInfo.set_overwrite(true);
                       var files = hostWebContext.get_web().getFolderByServerRelativeUrl(serverRelativeUrl).get_files();
                       var isexist = false;
                        jsonObject.d.results.forEach(function (item, index) {
                            if (item.Name == filename)
                                isexist = true;
                        });
                        if (isexist)
                            files.getByUrl(filename).checkOut();
                        var file = files.add(createInfo);
                        file.checkIn('Add-in deployment.');
                        file.publish('Add-in deployment.');

                        appWebContext.load(file);
                        appWebContext.executeQueryAsync(success, function (sender, args) {
                            alert(args.get_message());
                        });
                    },
                    error: function (data, errorCode, errorMessage) {
                        console.log("error");
                    }
                });
    };


Here, we are first fetching the file from app web and checking that whether it exists in host web or not. If the file exists then we will update the existing file, check in and publish it. If the file does not exist then we just add the file, check in and publish it.

2. Registering the custom action with the file path:

var context = web.get_context();
var webs = web.get_webs();
var actions = web.get_userCustomActions();
context.load(actions);
context.load(webs, 'Include(Title,Webs)');
context.executeQueryAsync(function () {

  var newAction = actions.add();
  // the 'description' is what we'll use to uniquely identify our custom action
  newAction.set_description(actionDescription);
  newAction.set_location('ScriptLink');
  newAction.set_scriptBlock(getScripts(window.hostWebUrl + '/Style Library/EasyNav/Scripts/tinysort.min.js'));
  newAction.set_sequence(1004);
  newAction.update();

  context.executeQueryAsync(function () { }, fail);
}, fail);

The above code adds a custom action on a web object in Host web. Wherein,
set_location specifies it is a script registration action,
set_scriptBlock indicates the JavaScript file's path in host web,
set_sequence specifies the sequence in which the files are to be loaded if there is more than one.

Once above code will be executed through custom action, we can see all JavaScript files loaded on all the pages of the SharePoint Site.

If you have any questions you can reach out our SharePoint Consulting team here.

Access Web App – Related Items Control - Parent/Child data in a Single View



Introduction

Today, I'll guide you through in detail how we can display and manage related data from 2 different tables, having parent/child relationship in a single view in SharePoint 2013 - Access Web app.
 
First, Create two tables: "Country" and "State". Now, if we want to view all States of the specific Country in Country Table View. That’s where the Related Item Control comes into picture!! Here, If you navigate to a record in Country table, you’ll see all States of that Country. You can also add other new States for that country from the same view.



Here are the steps to achieve the functionality:
  • Create "Country" and "State" tables with required fields in MS Access 2013.
  • For Country reference, add a lookup column which points to country name field in Country table, here it is ‘CID’.


  • Now, you can add data in Country and State tables and publish the app in SharePoint 2013. You can also insert same data from SharePoint List.

  • Country list view will look as below after adding data.
  • Add another control on list view called “Related Items Controls”, to show related state details on Country list view.
  • Configure it to display related state data as shown below.




  • Save view and launch the app.
  • Here, we can add new state details and/or also edit existing state data from this view.

If you have any questions you can reach out our SharePoint Consulting team here.