Giter Site home page Giter Site logo

sharepoint-angular-peoplepicker's Introduction

sharepoint-angular-peoplepicker

A client-side people picker control for SharePoint wrapped in an AngularJS directive.

SharePoint People Picker

##How To This people picker directive is based on the client side people picker included in the Office Dev/PnP code samples (https://github.com/OfficeDev/PnP). It is entirely JavaScript based, and can be used in an Angular application like so:

var app = angular.module('app', ['sp-peoplepicker']);

Then in your view, the directive can be instantiated like this:

<sp-people-picker name="taskAssignee" id="taskAssignee" ng-model="$scope.taskAssignees" min-entries="1" max-entries="5" allow-duplicates="false" show-login="false" show-title="true" min-characters="2" app-web-url="$scope.spAppWebUrl" />

If you are using bootstrap and want to add some validation to your form, you can use:

<form name="taskForm" role="form" ng-submit="$scope.ok(taskForm.$valid)" novalidate>
  <div class="form-group" ng-class="{ 'has-error' : taskForm.taskAssignee.$invalid && !taskForm.taskAssignee.$pristine }">
    <label for="taskAssignee">Assign To <span class="text-danger">*</span></label>
    <sp-people-picker name="taskAssignee" id="taskAssignee" ng-model="$scope.taskAssignees" max-entries="5" allow-duplicates="false" show-login="false" show-title="true" min-characters="2" min-entries="1" app-web-url="$scope.spAppWebUrl" />
    <p ng-show="taskForm.taskAssignee.$invalid && !taskForm.taskAssignee.$pristine" class="help-block">You must assign the task to at least 1, but not more than 5 people</p>
  </div>
</form>

To get the user values from the people picker and, for example, update a user field in SharePoint using the CSOM, you can do the following in your controller:

angular.forEach($scope.taskAssignees, function (value, key) {
    users.push(SP.FieldUserValue.fromUser(value.Login));
});
listItem.set_item('AssignedTo', users);
listItem.update();

By way of a longer example, you could update the Tasks list in your app's Host Web by invoking the following function from your controller:

function saveTask(task) {
  var dfd = $q.defer();
  var ctx = getSpCtx();
  var hostWeb = getHostWeb(ctx);
  var list = hostWeb.get_lists().getByTitle('Tasks');
  var listItemCreation = new SP.ListItemCreationInformation();
  var listItem = list.addItem(listItemCreation);
  listItem.set_item('Title', task.Title);
  listItem.set_item('Body', task.Body);
  listItem.set_item('DueDate', task.DueDate);
  listItem.set_item('_Category', task.Category);

  var users = [];
  angular.forEach(task.AssignedToId, function (value, key) {
      users.push(SP.FieldUserValue.fromUser(value.Login));
  });
  listItem.set_item('AssignedTo', users);
  listItem.update();
  ctx.load(listItem);
  ctx.executeQueryAsync(Function.createDelegate(this, saveComplete), Function.createDelegate(this, saveFailed));

  function saveComplete() {
      dfd.resolve(listItem);
  }

  function saveFailed(sender, args) {
      console.log("XHR failed for task create POST: " + args.get_message());
      dfd.reject(args.get_message());
  }

  return dfd.promise;
}

function getSpCtx() {
    // get SPAppWebUrl from the {StandardTokens} query parameters
    var ctx = new SP.ClientContext(spappcontext.hostWeb.SPAppWebUrl);
    var factory = new SP.ProxyWebRequestExecutorFactory(spappcontext.hostWeb.SPAppWebUrl);
    ctx.set_webRequestExecutorFactory(factory);
    return ctx;
}

function getHostWeb(ctx) {
    // get SPHostUrl from {StandardTokens} query parameters
    var hostWebctx = new SP.AppContextSite(ctx, spappcontext.hostWeb.SPHostUrl);
    var appWeb = ctx.get_web();
    var hostWeb = hostWebctx.get_web();
    return hostWeb;
}

##Arguments

  • ng-model: Specifies the variable in the controller that will get the array of resolved useds in the people picker
  • max-entries: The maximum number of people that an instance of the people picker will resolve. This is useful if you want to limit, say, the number of people that a task can be assigned to.
  • min-entries: The minimum number of people that an instance of the people picker will resolve. A value of one or greater has the effect of making the people picker a required field in the form.
  • allow-duplicates: Set to 'true' to allow duplicate names, otherwise set to 'false'
  • show-login: Set to 'true' to show the login name of users in the autocomplete dropdown
  • show-title: Set to 'true' to show the job title of users in the autocomplete dropdown, if available
  • min-characters: The minimum number of characters the user must type before the people picker control will attempt to find matching user names
  • app-web-url: The absolute URL to the App Web for the provider hosted app. More information on obtaining this below.

##Obtaining the App Web URL The App Web URL for a SharePoint provider hosted app is normally passed to the application as a query parameter. Most provider hosted applications will need to have access to this URL for making API calls back to the App Web. If you are not receiving the App Web URL in your query parameters, you will want to check the AppManifest.xml file for your application and ensure that the query string is configured to pass the '{StandardTokens}' expression.

sharepoint-angular-peoplepicker's People

Contributors

jasonvenema avatar patrickbussmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

sharepoint-angular-peoplepicker's Issues

Clear all people picker values on click

Hi all,
I have a need to clear the people picker values on click (all of them, not one at a time). Does anyone know how this would be achieved via JavaScript?
Thanks,
K.

Set value of People Picker to stored REST values

I am able to successfully get and store the values from the people picker using this directive, but I also have an update form where I need to display/resolve which people have been chosen. How would I set the people picker values on load? I have tried the following, but it doesn't resolve (where vm.cc is the name of my $scope tied to the directive).

 $scope.vm.cc = {
                Email: "[email protected]",
                Login: "i:0#.f|membership|[email protected]",
                Name: "Lastname, firstname"
            }

Thanks in advance for any assistance!

K.

Resolve SharePoint Groups

Hi all,
Using this directive, I can look up people, but it doesn't appear to resolve groups. Am I missing something?
Thanks,
K.

Using SharePoint Site instead of App Web

Great directive. Is it possible to use the URL of a SharePoint Site instead of the App URL? Example, if I am creating a responsive Web design and want to use a SharePoint Site versus App Model.

Thanks,
K.

Angular valdation fails when using directive

I have this directive in several forms, but when I have it placed, Angular form validation doesn't work. As an example, this button never becomes "enabled" even if all the fields, including the directive's, are filled in. Please assist!

<button type="button" data-ng-disabled="myForm.$invalid" data-ng-click="save()" class="btn btn-default form-control">SAVE ONLY</button>

Can't get scope/need IDs of users

I am attempting to get the scope of the people picker, but it is not resolving (undefined). Am I missing something obvious?

HTML:
sp-people-picker name="CC" id="CC" ng-model="$scope.cc" min-entries="1" max-entries="20" allow-duplicates="false" show-login="false" show-title="true" min-characters="2" app-web-url="$scope.spAppWebUrl" />

Here is the console log in my controller:

console.log($scope.cc);

Content Editor WP

I code more JS on SharePoint with CEWP than App Model. This version has a few changes to support that. Maybe these ideas could be worked into the original. Cheers!

Notes

  • live demo (cewp.html + cewp.js) to show usage
  • SP.ClientContext() [34-36] to open locally without any proxy to App Web URL. might be better to autodetect URL param "SPAppWebUrl" and then handle Context two ways depending if found.
  • maxSelectedPeople=3 param on directive to enforce a limit on how many people can be selected
  • HTML template autocomplete="off" to suppress IE dialogs

sp-peoplepicker.js.txt
cewp.html.txt
cewp.js.txt

Multiple People Pickers on one Page

Hi Jason,
I need to use multiple people pickers in one page. The first field works, but those after it don't. Ex:

<sp-people-picker name="CC7" id="CC7" ng-model="$scope.cc7" min-entries="1" max-entries="1" allow-duplicates="false" show-login="false" show-title="true" min-characters="2" app-web-url="$scope.spAppWebUrl" />

<sp-people-picker name="CC8" id="CC8" ng-model="$scope.cc8" min-entries="1" max-entries="1" allow-duplicates="false" show-login="false" show-title="true" min-characters="2" app-web-url="$scope.spAppWebUrl" />

Am I missing a method to allow multiple people pickers in a single form?

Thanks,

K.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.