Indeed i created (inspired from some blog posts written by people who knows things for real) those formulas for phone number and email:
- Phone (+39 080-1234567): =AND(
(FIND("+";[Phone];1)=1);
(ISNUMBER(VALUE(MID([Phone];2;FIND(" ";[Phone];1)-2))));
(ISNUMBER(VALUE(MID([Phone];FIND(" ";[Phone];1);FIND("-";[Phone];1)-FIND(" ";[Phone];1)))));
(ISNUMBER(VALUE(MID([Phone];FIND("-";[Phone];1)+1;LEN([Phone]))))))
- Email formula: =AND(NOT(ISERROR(FIND("@";[Email])));NOT(ISERROR(FIND(".";[Email])));ISERROR(FIND(" ";[Email]));NOT(ISERROR(FIND(".";[Email];FIND("@";[Email];1)))))
SharePoint validation does not provide regular expression support (doh!) so you need to have fun with excel formula. The pro is that you can unit test formulas with excel, the con is that you have less power than regex.
But this post deals with another problem: when you set validation on a document library and you try to create a folder via CSOM on it (maybe also with SSOM but i haven't tested it) you have a very fun error: "List data validation failed."
So what to do? The right code is the following one, indeed forcing the ContentTypeId during folder generation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | // Get content type instance ID ContentTypeCollection listContentTypes = list.ContentTypes; context.Load(listContentTypes, types => types.Include (type => type.Id, type => type.Name, type => type.Parent)); var result = context.LoadQuery(listContentTypes.Where (c => c.Name == "Folder" || c.Name == "Cartella" )); context.ExecuteQuery(); ContentType folderContentType = result.FirstOrDefault(); // Get author ID FieldUserValue userValue = new FieldUserValue(); userValue.LookupId = owner.Id; // Creating folder ListItemCreationInformation newItemInfo = new ListItemCreationInformation(); newItemInfo.UnderlyingObjectType = FileSystemObjectType.Folder; newItemInfo.LeafName = folderName; ListItem newListItem = list.AddItem(newItemInfo); newListItem[ "ContentTypeId" ] = folderContentType.Id.ToString(); newListItem[ "Author" ] = userValue; newListItem[ "Title" ] = folderName; newListItem.Update(); context.Load(list); context.ExecuteQuery(); |
1 2 3 4 5 6 | newListItem.BreakRoleInheritance( false , true ); RoleDefinitionBindingCollection role = new RoleDefinitionBindingCollection(context); role.Add(context.Web.RoleDefinitions.GetByType(RoleType.Reader)); newListItem.RoleAssignments.Add(owner, role); newListItem.Update(); context.ExecuteQuery(); |
Have fun.
0 commenti:
Post a Comment
Because of a lot of SPAM about courses, I need to moderate all comments here.
I ensure you that I will answer whenever possible (if you are not a spammer).