ヘルプが必要 - SharePoint 2010のタイプユーザーのリスト項目を更新する方法は?
-
16-10-2019 - |
質問
ユーザーのコンテンツタイプフィールドを持っているリストを更新しようとしています。ここでは、ユーザーの名前をPeople Pickerから保存しています。
ここで、私の要件は、そのリスト項目に特定の人を追加/削除することです。私はこのコードを試して人を追加していますが、他のすべてのユーザーを削除し、リストに新しいユーザーを追加しています。:(
ユーザーをグループに追加することはできますが、リスト項目に追加されていません。
これが私のコードスニペットです::
spweb myweb = site.openweb(); myweb.allowunsafeupdates = true; spgroupメンバーグループ= myweb.sitegroups ["groupName"];
if (SPContext.Current.Site.OpenWeb().CurrentUser != null && membersgroup != null)
{
membersgroup.AddUser(SPContext.Current.Site.OpenWeb().CurrentUser);
SPRoleDefinition role = myweb.RoleDefinitions["Contribute"];
SPRoleAssignment roleAssignment = new SPRoleAssignment(membersgroup);
roleAssignment.RoleDefinitionBindings.Add(role);
myweb.BreakRoleInheritance(true);
myweb.RoleAssignments.Add(roleAssignment);
myweb.Update();
SPList settingsLists = myweb.Lists["CommunitySettings"];
SPListItem itemToUpdate = settingsLists.Items[0];
itemToUpdate["MembersJoined"] = SPContext.Current.Site.OpenWeb().CurrentUser;
itemToUpdate.Update();
settingsLists.Update();
}
myweb.AllowUnsafeUpdates = false;
ユーザーを追加して削除するのを手伝ってください。
前もって感謝します。
これが答えです...
myweb.allowunsafeupdates = true;
SPGroup membersgroup = myweb.SiteGroups[string.Format("{0}-Member", myweb.Title)];
if (SPContext.Current.Site.OpenWeb().CurrentUser != null && membersgroup != null)
{
membersgroup.AddUser(SPContext.Current.Site.OpenWeb().CurrentUser);
SPRoleDefinition role = myweb.RoleDefinitions["Contribute"];
SPRoleAssignment roleAssignment = new SPRoleAssignment(membersgroup);
roleAssignment.RoleDefinitionBindings.Add(role);
myweb.BreakRoleInheritance(true);
myweb.RoleAssignments.Add(roleAssignment);
myweb.Update();
SPList settingsLists = myweb.Lists["CommunitySettings"];
SPListItem itemToUpdate = settingsLists.Items[0];
SPUser currentUser = SPContext.Current.Site.OpenWeb().CurrentUser;
SPFieldUserValue userValue = new SPFieldUserValue(myweb, currentUser.ID, currentUser.Name);
Microsoft.SharePoint.SPFieldUserValueCollection collection = (Microsoft.SharePoint.SPFieldUserValueCollection)itemToUpdate["MembersJoined"];
collection.Add(userValue);
itemToUpdate["MembersJoined"] = collection;
itemToUpdate.Update();
settingsLists.Update();
}
myweb.AllowUnsafeUpdates = false;
解決
OpenWeb()にそれらのすべての呼び出しを行うべきではありません - 廃棄パターンを確認してください。 Googleを検索すると、それに関する多くの情報があります。
アイテムに値を設定するために、私は 考える あなたはようなものが欲しいです
SPUser currentUser = ...
SPFieldUserValue f = new SPFieldUserValue();
f.User = currentUser;
item["MembersJoined"] = f;
他のヒント
USR SPFieldUserValue
クラス。
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfielduservalue.aspx
所属していません sharepoint.stackexchange