本文整理汇总了C#中Web.EnsureUser方法的典型用法代码示例。如果您正苦于以下问题:C# Web.EnsureUser方法的具体用法?C# Web.EnsureUser怎么用?C# Web.EnsureUser使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Web
的用法示例。
在下文中一共展示了Web.EnsureUser方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProvisionObjects
public override void ProvisionObjects(Web web, ProvisioningTemplate template)
{
// if this is a sub site then we're not provisioning security as by default security is inherited from the root site
if (web.IsSubSite())
{
return;
}
var siteSecurity = template.Security;
var ownerGroup = web.AssociatedOwnerGroup;
var memberGroup = web.AssociatedMemberGroup;
var visitorGroup = web.AssociatedVisitorGroup;
web.Context.Load(ownerGroup, o => o.Users);
web.Context.Load(memberGroup, o => o.Users);
web.Context.Load(visitorGroup, o => o.Users);
web.Context.ExecuteQueryRetry();
AddUserToGroup(web, ownerGroup, siteSecurity.AdditionalOwners);
AddUserToGroup(web, memberGroup, siteSecurity.AdditionalMembers);
AddUserToGroup(web, visitorGroup, siteSecurity.AdditionalVisitors);
foreach (var admin in siteSecurity.AdditionalAdministrators)
{
var user = web.EnsureUser(admin.Name);
user.IsSiteAdmin = true;
user.Update();
web.Context.ExecuteQueryRetry();
}
}
示例2: AddUserToGroup
private static void AddUserToGroup(Web web, Group group, List<User> members)
{
foreach (var user in members)
{
var existingUser = web.EnsureUser(user.Name);
group.Users.AddUser(existingUser);
}
web.Context.ExecuteQueryRetry();
}
示例3: ProvisionObjects
public override void ProvisionObjects(Web web, ProvisioningTemplate template)
{
Log.Info(Constants.LOGGING_SOURCE_FRAMEWORK_PROVISIONING, CoreResources.Provisioning_ObjectHandlers_SiteSecurity);
// if this is a sub site then we're not provisioning security as by default security is inherited from the root site
if (web.IsSubSite())
{
return;
}
var siteSecurity = template.Security;
var ownerGroup = web.AssociatedOwnerGroup;
var memberGroup = web.AssociatedMemberGroup;
var visitorGroup = web.AssociatedVisitorGroup;
web.Context.Load(ownerGroup, o => o.Users);
web.Context.Load(memberGroup, o => o.Users);
web.Context.Load(visitorGroup, o => o.Users);
web.Context.ExecuteQueryRetry();
if (!ownerGroup.ServerObjectIsNull.Value)
{
AddUserToGroup(web, ownerGroup, siteSecurity.AdditionalOwners);
}
if (!memberGroup.ServerObjectIsNull.Value)
{
AddUserToGroup(web, memberGroup, siteSecurity.AdditionalMembers);
}
if (!visitorGroup.ServerObjectIsNull.Value)
{
AddUserToGroup(web, visitorGroup, siteSecurity.AdditionalVisitors);
}
foreach (var admin in siteSecurity.AdditionalAdministrators)
{
var user = web.EnsureUser(admin.Name);
user.IsSiteAdmin = true;
user.Update();
web.Context.ExecuteQueryRetry();
}
}
示例4: AddReaderAccessImplementation
private static void AddReaderAccessImplementation(Web web, BuiltInIdentity user)
{
switch (user)
{
case BuiltInIdentity.Everyone:
{
string userIdentity = "c:0(.s|true";
var spReader = web.EnsureUser(userIdentity);
web.Context.Load(spReader);
web.Context.ExecuteQuery();
web.AssociatedVisitorGroup.Users.AddUser(spReader);
web.AssociatedVisitorGroup.Update();
web.Context.ExecuteQuery();
break;
}
case BuiltInIdentity.EveryoneButExternalUsers:
{
string userIdentity = string.Format("c:0-.f|rolemanager|spo-grid-all-users/{0}", web.GetAuthenticationRealm());
var spReader = web.EnsureUser(userIdentity);
web.Context.Load(spReader);
web.Context.ExecuteQuery();
web.AssociatedVisitorGroup.Users.AddUser(spReader);
web.AssociatedVisitorGroup.Update();
web.Context.ExecuteQuery();
break;
}
}
}
示例5: ProvisionObjects
public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation)
{
using (var scope = new PnPMonitoredScope(this.Name))
{
if (template.Lists.Any())
{
var rootWeb = (web.Context as ClientContext).Site.RootWeb;
web.EnsureProperties(w => w.ServerRelativeUrl);
web.Context.Load(web.Lists, lc => lc.IncludeWithDefaultProperties(l => l.RootFolder.ServerRelativeUrl));
web.Context.ExecuteQueryRetry();
var existingLists = web.Lists.AsEnumerable<List>().Select(existingList => existingList.RootFolder.ServerRelativeUrl).ToList();
var serverRelativeUrl = web.ServerRelativeUrl;
#region DataRows
foreach (var listInstance in template.Lists)
{
if (listInstance.DataRows != null && listInstance.DataRows.Any())
{
scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows_Processing_data_rows_for__0_, listInstance.Title);
// Retrieve the target list
var list = web.Lists.GetByTitle(listInstance.Title);
web.Context.Load(list);
// Retrieve the fields' types from the list
FieldCollection fields = list.Fields;
web.Context.Load(fields, fs => fs.Include(f => f.InternalName, f => f.FieldTypeKind));
web.Context.ExecuteQueryRetry();
foreach (var dataRow in listInstance.DataRows)
{
try
{
scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows_Creating_list_item__0_, listInstance.DataRows.IndexOf(dataRow) + 1);
var listitemCI = new ListItemCreationInformation();
var listitem = list.AddItem(listitemCI);
foreach (var dataValue in dataRow.Values)
{
Field dataField = fields.FirstOrDefault(
f => f.InternalName == parser.ParseString(dataValue.Key));
if (dataField != null)
{
String fieldValue = parser.ParseString(dataValue.Value);
switch (dataField.FieldTypeKind)
{
case FieldType.Geolocation:
// FieldGeolocationValue - Expected format: Altitude,Latitude,Longitude,Measure
var geolocationArray = fieldValue.Split(',');
if (geolocationArray.Length == 4)
{
var geolocationValue = new FieldGeolocationValue
{
Altitude = Double.Parse(geolocationArray[0]),
Latitude = Double.Parse(geolocationArray[1]),
Longitude = Double.Parse(geolocationArray[2]),
Measure = Double.Parse(geolocationArray[3]),
};
listitem[parser.ParseString(dataValue.Key)] = geolocationValue;
}
else
{
listitem[parser.ParseString(dataValue.Key)] = fieldValue;
}
break;
case FieldType.Lookup:
// FieldLookupValue - Expected format: LookupID
var lookupValue = new FieldLookupValue
{
LookupId = Int32.Parse(fieldValue),
};
listitem[parser.ParseString(dataValue.Key)] = lookupValue;
break;
case FieldType.URL:
// FieldUrlValue - Expected format: URL,Description
var urlArray = fieldValue.Split(',');
var linkValue = new FieldUrlValue();
if (urlArray.Length == 2)
{
linkValue.Url = urlArray[0];
linkValue.Description = urlArray[1];
}
else
{
linkValue.Url = urlArray[0];
linkValue.Description = urlArray[0];
}
listitem[parser.ParseString(dataValue.Key)] = linkValue;
break;
case FieldType.User:
// FieldUserValue - Expected format: loginName
var user = web.EnsureUser(fieldValue);
web.Context.Load(user);
web.Context.ExecuteQueryRetry();
//.........这里部分代码省略.........
示例6: AddUserToGroup
private static void AddUserToGroup(Web web, Group group, List<User> members, PnPMonitoredScope scope)
{
if (members.Any())
{
scope.LogDebug("Adding users to group {0}", group.Title);
try
{
foreach (var user in members)
{
scope.LogDebug("Adding user {0}", user.Name);
var existingUser = web.EnsureUser(user.Name);
group.Users.AddUser(existingUser);
}
web.Context.ExecuteQueryRetry();
}
catch (Exception ex)
{
scope.LogError(CoreResources.Provisioning_ObjectHandlers_SiteSecurity_Add_users_failed_for_group___0_____1_____2_, group.Title, ex.Message, ex.StackTrace);
throw;
}
}
}
示例7: ProvisionObjects
public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation)
{
using (var scope = new PnPMonitoredScope(this.Name))
{
// if this is a sub site then we're not provisioning security as by default security is inherited from the root site
if (web.IsSubSite())
{
scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_SiteSecurity_Context_web_is_subweb__skipping_site_security_provisioning);
return parser;
}
var siteSecurity = template.Security;
var ownerGroup = web.AssociatedOwnerGroup;
var memberGroup = web.AssociatedMemberGroup;
var visitorGroup = web.AssociatedVisitorGroup;
web.Context.Load(ownerGroup, o => o.Title, o => o.Users);
web.Context.Load(memberGroup, o => o.Title, o => o.Users);
web.Context.Load(visitorGroup, o => o.Title, o => o.Users);
web.Context.ExecuteQueryRetry();
if (!ownerGroup.ServerObjectIsNull.Value)
{
AddUserToGroup(web, ownerGroup, siteSecurity.AdditionalOwners, scope);
}
if (!memberGroup.ServerObjectIsNull.Value)
{
AddUserToGroup(web, memberGroup, siteSecurity.AdditionalMembers, scope);
}
if (!visitorGroup.ServerObjectIsNull.Value)
{
AddUserToGroup(web, visitorGroup, siteSecurity.AdditionalVisitors, scope);
}
foreach (var siteGroup in siteSecurity.SiteGroups)
{
Group group = null;
var allGroups = web.Context.LoadQuery(web.SiteGroups.Include(gr => gr.LoginName));
web.Context.ExecuteQueryRetry();
if (!web.GroupExists(siteGroup.Title))
{
scope.LogDebug("Creating group {0}", siteGroup.Title);
group = web.AddGroup(
parser.ParseString(siteGroup.Title),
parser.ParseString(siteGroup.Description),
parser.ParseString(siteGroup.Title) == parser.ParseString(siteGroup.Owner));
group.AllowMembersEditMembership = siteGroup.AllowMembersEditMembership;
group.AllowRequestToJoinLeave = siteGroup.AllowRequestToJoinLeave;
group.AutoAcceptRequestToJoinLeave = siteGroup.AutoAcceptRequestToJoinLeave;
if (parser.ParseString(siteGroup.Title) != parser.ParseString(siteGroup.Owner))
{
Principal ownerPrincipal = allGroups.FirstOrDefault(gr => gr.LoginName == parser.ParseString(siteGroup.Owner));
if (ownerPrincipal == null)
{
ownerPrincipal = web.EnsureUser(parser.ParseString(siteGroup.Owner));
}
group.Owner = ownerPrincipal;
}
group.Update();
web.Context.ExecuteQueryRetry();
}
else
{
group = web.SiteGroups.GetByName(parser.ParseString(siteGroup.Title));
web.Context.Load(group,
g => g.Title,
g => g.Description,
g => g.AllowMembersEditMembership,
g => g.AllowRequestToJoinLeave,
g => g.AutoAcceptRequestToJoinLeave,
g => g.Owner.LoginName);
web.Context.ExecuteQueryRetry();
var isDirty = false;
if (group.Description != parser.ParseString(siteGroup.Description))
{
group.Description = parser.ParseString(siteGroup.Description);
isDirty = true;
}
if (group.AllowMembersEditMembership != siteGroup.AllowMembersEditMembership)
{
group.AllowMembersEditMembership = siteGroup.AllowMembersEditMembership;
isDirty = true;
}
if (group.AllowRequestToJoinLeave != siteGroup.AllowRequestToJoinLeave)
{
group.AllowRequestToJoinLeave = siteGroup.AllowRequestToJoinLeave;
isDirty = true;
}
if (group.AutoAcceptRequestToJoinLeave != siteGroup.AutoAcceptRequestToJoinLeave)
{
group.AutoAcceptRequestToJoinLeave = siteGroup.AutoAcceptRequestToJoinLeave;
isDirty = true;
}
if (group.Owner.LoginName != parser.ParseString(siteGroup.Owner))
//.........这里部分代码省略.........
示例8: AddReaderAccessImplementation
private static User AddReaderAccessImplementation(Web web, BuiltInIdentity user)
{
switch (user)
{
case BuiltInIdentity.Everyone:
{
const string userIdentity = "c:0(.s|true";
User spReader = web.EnsureUser(userIdentity);
web.Context.Load(spReader);
web.Context.ExecuteQueryRetry();
web.AssociatedVisitorGroup.Users.AddUser(spReader);
web.AssociatedVisitorGroup.Update();
web.Context.ExecuteQueryRetry();
return spReader;
}
case BuiltInIdentity.EveryoneButExternalUsers:
{
User spReader = null;
try
{
// New tenant
string userIdentity = string.Format("c:0-.f|rolemanager|spo-grid-all-users/{0}", web.GetAuthenticationRealm());
spReader = web.EnsureUser(userIdentity);
web.Context.Load(spReader);
web.Context.ExecuteQueryRetry();
}
catch (ServerException)
{
// old tenant?
string userIdentity = string.Empty;
web.Context.Load(web, w => w.Language);
web.Context.ExecuteQueryRetry();
switch (web.Language)
{
case 1025: // Arabic
userIdentity = "الجميع باستثناء المستخدمين الخارجيين";
break;
case 1069: // Basque
userIdentity = "Guztiak kanpoko erabiltzaileak izan ezik";
break;
case 1026: // Bulgarian
userIdentity = "Всички освен външни потребители";
break;
case 1027: // Catalan
userIdentity = "Tothom excepte els usuaris externs";
break;
case 2052: // Chinese (Simplified)
userIdentity = "除外部用户外的任何人";
break;
case 1028: // Chinese (Traditional)
userIdentity = "外部使用者以外的所有人";
break;
case 1050: // Croatian
userIdentity = "Svi osim vanjskih korisnika";
break;
case 1029: // Czech
userIdentity = "Všichni kromě externích uživatelů";
break;
case 1030: // Danish
userIdentity = "Alle undtagen eksterne brugere";
break;
case 1043: // Dutch
userIdentity = "Iedereen behalve externe gebruikers";
break;
case 1033: // English
userIdentity = "Everyone except external users";
break;
case 1061: // Estonian
userIdentity = "Kõik peale väliskasutajate";
break;
case 1035: // Finnish
userIdentity = "Kaikki paitsi ulkoiset käyttäjät";
break;
case 1036: // French
userIdentity = "Tout le monde sauf les utilisateurs externes";
break;
case 1110: // Galician
userIdentity = "Todo o mundo excepto os usuarios externos";
break;
case 1031: // German
userIdentity = "Jeder, außer externen Benutzern";
break;
case 1032: // Greek
userIdentity = "Όλοι εκτός από εξωτερικούς χρήστες";
break;
case 1037: // Hebrew
userIdentity = "כולם פרט למשתמשים חיצוניים";
break;
case 1081: // Hindi
userIdentity = "बाह्य उपयोगकर्ताओं को छोड़कर सभी";
break;
case 1038: // Hungarian
userIdentity = "Mindenki, kivéve külső felhasználók";
break;
case 1057: // Indonesian
userIdentity = "Semua orang kecuali pengguna eksternal";
break;
//.........这里部分代码省略.........
示例9: ResolvePrincipal
private Principal ResolvePrincipal(ClientRuntimeContext context, Web web, string owner)
{
Principal result = null;
var targetSources = new Dictionary<PrincipalType, PrincipalInfo>();
// owner might be only a user or sharepoint group
// making a few attempts and checking NULL ref later in the code
targetSources.Add(PrincipalType.SharePointGroup, null);
targetSources.Add(PrincipalType.User, null);
foreach (var targetSource in targetSources.Keys)
{
// ResolvePrincipal != SearchPrincipals, at all!
//var principalInfos = Utility.ResolvePrincipal(context, web, owner, targetSource, PrincipalSource.All, null, false);
var principalInfos = Utility.SearchPrincipals(context, web, owner, targetSource, PrincipalSource.All, null, 2);
context.ExecuteQueryWithTrace();
if (principalInfos.Count > 0)
//if (principalInfos.Value != null)
{
var info = principalInfos[0];
//var info = principalInfos.Value;
targetSources[targetSource] = info;
if (targetSource == PrincipalType.User || targetSource == PrincipalType.SecurityGroup)
result = web.EnsureUser(info.LoginName);
if (targetSource == PrincipalType.SharePointGroup)
result = web.SiteGroups.GetById(info.PrincipalId);
context.Load(result);
context.ExecuteQueryWithTrace();
// nic, found, break, profit!
break;
}
}
return result;
}
示例10: ProvisionObjects
public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation)
{
using (var scope = new PnPMonitoredScope(CoreResources.Provisioning_ObjectHandlers_SiteSecurity))
{
// if this is a sub site then we're not provisioning security as by default security is inherited from the root site
if (web.IsSubSite())
{
scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_SiteSecurity_Context_web_is_subweb__skipping_site_security_provisioning);
return parser;
}
var siteSecurity = template.Security;
var ownerGroup = web.AssociatedOwnerGroup;
var memberGroup = web.AssociatedMemberGroup;
var visitorGroup = web.AssociatedVisitorGroup;
web.Context.Load(ownerGroup, o => o.Users);
web.Context.Load(memberGroup, o => o.Users);
web.Context.Load(visitorGroup, o => o.Users);
web.Context.ExecuteQueryRetry();
if (!ownerGroup.ServerObjectIsNull.Value)
{
AddUserToGroup(web, ownerGroup, siteSecurity.AdditionalOwners,scope);
}
if (!memberGroup.ServerObjectIsNull.Value)
{
AddUserToGroup(web, memberGroup, siteSecurity.AdditionalMembers,scope);
}
if (!visitorGroup.ServerObjectIsNull.Value)
{
AddUserToGroup(web, visitorGroup, siteSecurity.AdditionalVisitors,scope);
}
foreach (var admin in siteSecurity.AdditionalAdministrators)
{
var user = web.EnsureUser(admin.Name);
user.IsSiteAdmin = true;
user.Update();
web.Context.ExecuteQueryRetry();
}
}
return parser;
}
示例11: ProvisionObjects
public override void ProvisionObjects(Web web, ProvisioningTemplate template, ProvisioningTemplateApplyingInformation applyingInformation)
{
Log.Info(Constants.LOGGING_SOURCE_FRAMEWORK_PROVISIONING, CoreResources.Provisioning_ObjectHandlers_ListInstancesDataRows);
if (template.Lists.Any())
{
var rootWeb = (web.Context as ClientContext).Site.RootWeb;
if (!web.IsPropertyAvailable("ServerRelativeUrl"))
{
web.Context.Load(web, w => w.ServerRelativeUrl);
web.Context.ExecuteQueryRetry();
}
web.Context.Load(web.Lists, lc => lc.IncludeWithDefaultProperties(l => l.RootFolder.ServerRelativeUrl));
web.Context.ExecuteQueryRetry();
var existingLists = web.Lists.AsEnumerable<List>().Select(existingList => existingList.RootFolder.ServerRelativeUrl).ToList();
var serverRelativeUrl = web.ServerRelativeUrl;
#region DataRows
foreach (var listInstance in template.Lists)
{
if (listInstance.DataRows != null && listInstance.DataRows.Any())
{
// Retrieve the target list
var list = web.Lists.GetByTitle(listInstance.Title);
web.Context.Load(list);
// Retrieve the fields' types from the list
FieldCollection fields = list.Fields;
web.Context.Load(fields, fs => fs.Include(f => f.InternalName, f => f.FieldTypeKind));
web.Context.ExecuteQueryRetry();
foreach (var dataRow in listInstance.DataRows)
{
var listitemCI = new ListItemCreationInformation();
var listitem = list.AddItem(listitemCI);
foreach (var dataValue in dataRow.Values)
{
Field dataField = fields.FirstOrDefault(
f => f.InternalName == dataValue.Key.ToParsedString());
if (dataField != null)
{
String fieldValue = dataValue.Value.ToParsedString();
switch (dataField.FieldTypeKind)
{
case FieldType.Geolocation:
// FieldGeolocationValue - Expected format: Altitude,Latitude,Longitude,Measure
var geolocationArray = fieldValue.Split(',');
if (geolocationArray.Length == 4)
{
var geolocationValue = new FieldGeolocationValue
{
Altitude = Double.Parse(geolocationArray[0]),
Latitude = Double.Parse(geolocationArray[1]),
Longitude = Double.Parse(geolocationArray[2]),
Measure = Double.Parse(geolocationArray[3]),
};
listitem[dataValue.Key.ToParsedString()] = geolocationValue;
}
else
{
listitem[dataValue.Key.ToParsedString()] = fieldValue;
}
break;
case FieldType.Lookup:
// FieldLookupValue - Expected format: LookupID
var lookupValue = new FieldLookupValue
{
LookupId = Int32.Parse(fieldValue),
};
listitem[dataValue.Key.ToParsedString()] = lookupValue;
break;
case FieldType.URL:
// FieldUrlValue - Expected format: URL,Description
var urlArray = fieldValue.Split(',');
var linkValue = new FieldUrlValue();
if (urlArray.Length == 2)
{
linkValue.Url = urlArray[0];
linkValue.Description = urlArray[1];
}
else
{
linkValue.Url = urlArray[0];
linkValue.Description = urlArray[0];
}
listitem[dataValue.Key.ToParsedString()] = linkValue;
break;
case FieldType.User:
// FieldUserValue - Expected format: loginName
var user = web.EnsureUser(fieldValue);
web.Context.Load(user);
web.Context.ExecuteQueryRetry();
if (user != null)
{
//.........这里部分代码省略.........