Drill down to current user by default in Org Chart

Note

For the versions earlier than 4.x.x (including on-premises ones), please follow this instruction.

Drill down to current user on Org Chart load for User Profiles

It is quite simple to implement such drill down for User Profiles data source using JavaScript framework. Just open the configuration wizard and paste the code below to JavaScript editor on Custom JavaScript step.

api.config.renderInitialNode = false;

api.onInitialLoadingFinished(() => {
  api.dataProvider.getCurrentUserAccountName(function (accountName) {
    api.drillDown(accountName);
  });
});

Drill down to current user on Org Chart load for SharePoint list

It is more difficult to implement such behavior for SharePoint list. First of all we have to ensure that we have field with account name of user in our SharePoint list. In my case I created list field called “AccountName”. Then I used JSOM to find list item by account name of current user. I used CAML query to do this. Once I have list item for current user I can read ID of for this item and drill down to it using JavaScript framework. Actually you can just copy and paste the code below to JavaScript editor on Custom JavaScript step of the configuration wizard. The only thing you may need to replace is internal name of field where account name of user is stored in SharePoint list. Just replace AccountName with your field internal name:

let loginFieldInternalName = "AccountName";

And it is complete script to copy paste:

const getCurrentUserOrgChartId = (completed, error) => {
  const camlQueryTemplate = "<View><Query><Where><Eq><FieldRef Name='{{loginFieldInternalName}}'/><Value Type='Text'>{{currentUserLogin}}</Value></Eq></Where></Query></  View>";
  const listId = renderer.config.listDataSourceSettings.listId;
  const orgChartIdFieldName = renderer.config.idFieldMapping.InternalFieldName;

  const context = SP.ClientContext.get_current();

  api.dataProvider.getCurrentUserAccountName((currentUserLogin) => {
    console.log("currentUserLogin:", currentUserLogin);
    currentUserLogin = currentUserLogin.replace(/.*\|/, "");

    const list = context.get_web().get_lists().getById(listId);
    const camlQuery = new SP.CamlQuery();
    const queryText = camlQueryTemplate
      .replace("{{loginFieldInternalName}}", loginFieldInternalName)
      .replace("{{currentUserLogin}}", currentUserLogin);

    console.log("queryText: ", queryText);

    camlQuery.set_viewXml(queryText);
    const foundItems = list.getItems(camlQuery);

    context.load(foundItems);

    context.executeQueryAsync(() => {
      f = foundItems;
      const en = foundItems.getEnumerator();
      if (en.moveNext()) {
        const fieldValuesForCurrentUser = en.get_current().get_fieldValues();
        const currentUserOrgChartId = fieldValuesForCurrentUser[orgChartIdFieldName];

        completed(currentUserOrgChartId);
      } else {
        console.log("List item for current user not found.");
      }
    }, (sender, args) => {
      error(args);
    });
  });

}

let currentUserId = "";
let loginFieldInternalName = "AccountName";

api.prerenderAction = function (completed) {
  getCurrentUserOrgChartId((userId) => {
    currentUserId = userId;
    completed();
  }, (errorArgs) => {
    console.log(errorArgs);
    completed();
  });
}

api.onInitialLoadingFinished(() => {
  if (currentUserId) {
    api.drillDown(currentUserId);
  }
});