To save a user name into a people field:
Provided a people editor control in my custom form and saved each resolved entity as follows:
if (currentPeopleEditor.Entities.Count != 0)
{
SPFieldUserValueCollection userCollection = new SPFieldUserValueCollection();
for (int index = 0; index < currentPeopleEditor.ResolvedEntities.Count; index++)
{
PickerEntity ObjEntity = (PickerEntity)currentPeopleEditor.ResolvedEntities[index];
userCollection.Add(new SPFieldUserValue(objSPWeb,Convert.ToInt32(ObjEntity.EntityData["SPUserID"]), ObjEntity.Key));
}
newItem[Field.Key.ToString()] = userCollection;
}
It was working very fine until some user stated getting this exception:
"Invalid Look-up Value
A look-up field contains invalid data, Please check the value and try again."
On investigating we found that this error was occurring because
ObjEntity.EntityData["SPUserID"]) was returning as null.
It was happening because the requirement is to save some users name who don't access this site collection but they are member of corporate AD system.
To resolve this issue we looked up to SharePoint and replicated what they do.
In one of the list add a column of type people and group allowed it to pick any user.
create new item in this list and Pick a user whose name is not present in all users list.
save the item.
go back to all users list you will see that new user name got added to this list.
Note: To see all users list in UI browse to following location:
site collection URL/_catalogs/users/simple.aspx
so now to achieve the same functionality. I updated the code as follows
if (currentPeopleEditor.Entities.Count != 0)
{
SPFieldUserValueCollection userCollection = new SPFieldUserValueCollection();
for (int index = 0; index < currentPeopleEditor.ResolvedEntities.Count; index++)
{
PickerEntity ObjEntity = (PickerEntity)currentPeopleEditor.ResolvedEntities[index];
if (ObjEntity.EntityData["SPUserID"] == null)
{
SPContext.Current.Site.RootWeb.AllUsers.Add(ObjEntity.Key, ObjEntity.EntityData["Email"].ToString(), ObjEntity.DisplayText, "");
SPContext.Current.Site.RootWeb.Update();
userCollection.Add(new SPFieldUserValue(objSPWeb, Convert.ToInt32(SPContext.Current.Site.RootWeb.AllUsers[ObjEntity.Key].ID), ObjEntity.Key));
}
else
{ userCollection.Add(new SPFieldUserValue(objSPWeb, Convert.ToInt32(ObjEntity.EntityData["SPUserID"]), ObjEntity.Key));
}
}
newItem[Field.Key.ToString()] = userCollection;
}
Long story short to resolve above problem in SharePoint 2010 just use
web.EnsureUser()
It adds user to the all user list and returns spuser object.
Related
I have a Dynamics entity that has a relationship to another entity with a Main form in UX. I have a lookup control on the form to select that related entity. Doing so correctly writes the ID of that related entity record to the current record being edited. I need help grabbing the name value of that linked entity and writing it to the name value of the current record when the lookup selection changes. The form designer makes this so much more difficult than it needs to be! Is there a set of basic jscript libraries that I can add to my solution and call for such simple tasks?
After literally HOURS of hunting down bits and pieces, I finally got this to work!
function updateName(){
var name = "";
var lookupField = Xrm.Page.getAttribute("lookupentityproperty");
// Verify the field does exist on the form and has a selected value
if (lookupField != null && lookupField.getValue() != null && lookupField.getValue()[0] != null) {
name = lookupField.getValue()[0].name;
}
else { name = null; }
var nameField= Xrm.Page.getAttribute("nameproperty");
nameField.setValue(name);
}
I want to find out if current Outlook user is a member of particular exchange distribution list. If he is, then he should see child form and if he isn't; then he should see message box.
My following code is working up to the point, if user is a member of DistList, he get child form but I don't know how to check show him message box if he isn't member.
string UserName = (string)application.ActiveExplorer().Session.CurrentUser.Name;
string PersonalPublicFolder = "Public Folders - " + application.ActiveExplorer().Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress;
Outlook.MAPIFolder contactsFolder = outlookNameSpace.Folders[PersonalPublicFolder].Folders["Favorites"];
Outlook.DistListItem addressList = contactsFolder.Items["ContactGroup"];
if (addressList.MemberCount != 0)
{
for (int i = 1; i <= addressList.MemberCount; i++)
{
Outlook.Recipient recipient = addressList.GetMember(i);
string contact = recipient.Name;
if (contact == UserName)
{
var assignOwnership = new AssignOwnership();
assignOwnership.Show();
}
}
}
Any help would be appreciated.
Thank you.
Use Application.Session.CurrentUser.AddressEntry.GetExchangeUser().GetMemberOfList() - it will return AddressEntries object that contains all DLs that the user is a member of.
Be prepared to handle nulls and errors.
I'm new to Knockout, and I'm trying to use it on a page showing a system's users and the roles that each user has.
The data is in an observableArray of users. The user object has a roles property, which is another observableArray. This second array contains an object for each role, with an ID and a boolean "granted" property.
I want to be able to display all of the users with a specific role, so there's a checkbox for each role - when one of these is checked, the list should show the users with that role.
The problem I've got is that filtering the 1,000 or so users by roles takes several seconds. Filtering by the text in the name is very quick (a few milliseconds), but filtering by role is not. I've put some timing code in, and the issue is the method I'm using to check whether the user has the selected role(s) so I'm just wondering whether there's a better way of doing it, maybe using some Knockout magic.
Below is the ko.computed on the view model that I'm using to do the filtering. The results table is bound to this function.
self.filteredUsers = ko.computed(function () {
var textFilter = self.filter(); // this is an observable bound to a text field
var checkedRoles = self.selectedRoles(); // this is a computed, which returns an array of checked roles
return ko.utils.arrayFilter(self.users(), function (user) {
var match = true;
if (user.displayName.toLowerCase().indexOf(textFilter.toLowerCase()) == -1) {
match = false;
}
// for each ticked role, check the user has the role
for (var i = 0; i < checkedRoles.length; i++) {
var roleMatch = false;
for (var j = 0; j < user.roles().length; j++) {
if (user.roles()[j].roleId === checkedRoles[i].roleId && user.roles()[j].granted()) {
roleMatch = true;
break;
}
}
if (!roleMatch) {
match = false;
}
}
return match;
});
});
I think that a good optimization would be creating a grantedRoles computed on your user object. This computed would return an object that you can use as an index, would contain properties keyed by a role's unique identifier and would only contain roles that are granted.
Then in filteredUsers, you would check the grantedRoles object against each checked role, rather than looping through user.roles() for each checked role.
I have a site I'm porting to MVC to clean up the code and simplify things. I use the asp membership and profile providers, but I'm wondering if I'm doing this correctly for my situtation. I'm pretty new to MVC, so I wan to get this right in the early stages.
Users are individuals and they are part of larger "institutions" that they either set up or pick at registration. In this case, the institution is a winery. I want the users to be able to view all wines from every winery, but only edit ones that belong to them.
What's the best way to do this? Right now I render the link to the edit field in my index view based on their instution ID and the producer ID. I feel like a data annotation might work better here, but I don't exactly how to implement that for a group of wines. Do I need multiple providers? I use roles to limit the editing, but right now an editor role could manually enter the path of another wine to edit it when that wine doesn't belong to them.
Any pointers here would be awesome. I know I can do it in the controller methods, but I'm looking for the 'right' way to do it. Thanks.
I'm running into the same issue at work right now, and the best proposed solution we have right now is implementing an "ownership" table. You won't be able to solve this using roles.
So basically you have an owner ID, owned object's ID, and the type of objects ID all held together. Lets take an edit request for example. We know that you can only edit the data person X owns, so we have a stored procedure that if a key combination exists in our ownership table where person.ID = owner ID, and item.ID = object ID, and item.TypeID = objectTypeID. If it exists, it goes along performing its edits, otherwise it returns an error.
You can use this scheme to return ownership lists, user validation, and a host of other issues you may come across. You probably won't need the ObjectTypeID if you only have one type's ownership being tracked. Hope this helps!
I figured this out by applying a custom AuthorizeAttribute to the edit, delete, and create actions.
Here is what I ended up doing:
public class ProducerEditAttribute : AuthorizeAttribute
{
private vfContext db = new vfContext();
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
bool bAdmin = filterContext.HttpContext.User.IsInRole("admin");
bool bProdEdit = filterContext.HttpContext.User.IsInRole("producereditor");
bool bProd = filterContext.HttpContext.User.IsInRole("producer");
if (filterContext.HttpContext.User.Identity.IsAuthenticated)
{
if (bAdmin)
{
//authorized
}
if (bProdEdit || bProd)
{
//check for current wine belonging to the producer.
Producer currentProd = db.Producers.Find(Profile.GetProfile(filterContext.HttpContext.User.Identity.Name).InstitutionID);
Wine currentWine;
object WineID;
if (filterContext.RouteData.Values.TryGetValue("id", out WineID))
{
currentWine = db.Wines.Find(int.Parse(WineID.ToString()));
if (currentProd.Wines.Contains(currentWine) && bProdEdit)
{
//authorized
}
else if (bProd)
{
var result = new ViewResult();
result.ViewName = "Login.cshtml"; //this can be a property you don't have to hard code it
result.MasterName = "_Layout.cshtml"; //this can also be a property
result.ViewBag.Message = "You do not have permission for add/edit/delete funciontionality. Please request.";
filterContext.Result = result;
}
else
{
var result = new ViewResult();
result.ViewName = "Login.cshtml";
result.MasterName = "_Layout.cshtml";
filterContext.Result = result;
}
}
}
else
{
var result = new ViewResult();
result.ViewName = "Login.cshtml";
result.MasterName = "_Layout.cshtml";
}
}
}
}
Say you're writing a web page for fruit vendors using Spring MVC's SimpleFormController, version 2.5.6. On this page the vendor can do simple things like change their name or their address. They can also change their inventory based on a drop down list filled with present inventory selections.
When this drop down list selection changes, the entire form changes to match the inventory of what has been selected. So one stock selection may have bananas and pears, another may have melons, blueberries and grapefruit.
Inside each inventory selection is a input field that needs to be propagated back to the database, for the sake of this example let's say that the user enters the number of fruit.
The way this is modeled in the database is that each Stock name is stored in a table, which has a one to many relationship with the contents of each stock, which would be the type of fruit in this example. Then the type of fruit has a one to many relationship with the quantity the vendor selects. Stock name and the type of fruit in each stock are stored in the database and are unchangeable by the user, with the connected fruit quantity table being editable.
My question is, how do you model the form described above in Spring MVC?
I've tried overriding the isFormChangeRequest and onFormChange to facilitate the form change, but I think I may be misunderstanding the intent of these methods. When I change my backing command object the next time the page is post it tries to bind the request into the form, which breaks if you adjust the size of the Stock array (say from 3 to 2, it will try and bind into the 3rd value, even if it is empty).
If you have a limited amount of different stocks, you can use different handler mappings for each one with a different backing model:
#RequestMapping(params="stock=example1")
ModelAndView handleExample1(#ModelAttribute("stock") ApplesOrangesPears stockObject)
#RequestMapping(params="stock=example2")
ModelAndView handleExample2(#ModelAttribute("stock") BananasPotatos stockObject)
But I guess that is not the case, there are a lot of different stock types and they are dynamic. In that case you can register custom property editor (#InitBinder), and determine dynamically the actual type of the backing object for the inventory, then validate, and convert to or from it explicitly.
What I ended up doing is firing a JavaScript event when the selection in the drop down is changed. This JavaScript (seen below) generates a URL based on the selection of the drop down and uses a location.replace to go to the new URL, which causes the controller to generate a new form.
Using this method over overriding the isFormChangeRequest and onFormChange has allowed me to avoid binding errors caused by left over post data.
function changeUrl(selectionValue) {
var param = getParams();
param["dropdownselection"] = selectionValue;
window.location.replace(getBaseUrl() + buildQueryString(param));
}
//taken from http://javascript.about.com/library/blqs1.htm
function getParams() {
var qsParm = new Array();
var query = window.location.search.substring(1);
var parms = query.split('&');
for (var i = 0; i < parms.length; i++) {
var pos = parms[i].indexOf('=');
if (pos > 0) {
var key = parms[i].substring(0,pos);
var val = parms[i].substring(pos+1);
qsParm[key] = val;
}
}
return qsParm;
}
function getBaseUrl() {
var url = document.location.toString();
if (url.indexOf('?') != -1) {
url = url.substring(0, url.indexOf('?'));
}
return url;
}
function buildQueryString(param) {
var queryString = "?";
for (var key in param) {
queryString += key + "=" + param[key] + "&";
}
//remove last "&"
return queryString.substring(0,queryString.length - 1);
}