我的用例:

我正在尝试使用Visual Web部件来实现事件管理系统到SharePoint。

我有两个自定义列表,一个被称为“事件”。假设它有一个列“标题”,它显示了显示形式中的事件详细信息(其在框中标题字段,实际上)以及其他列中的其他一些非相关细节。

第二个列表称为“与会者”。有两列,第一列:“参加者”(用户字段获取当前用户的LoginName)。第二:“事件”(目前:事件标题的字符串)。

在我的Visual WebPart(通过&toolpaneview= 2上面的显示形式的“事件”--list)我显示按钮(“寄存器”和“取消注册”),具体取决于以下代码:

...
using System.Linq;
...
//Snippet of prerequisites for better understanding
SPWeb web = SPContext.Current.Web;
SPUser currentUser = web.CurrentUser;
SPListItem currentEventItem = (SPListItem)SPContext.Current.Item;
string userString = currentUser.ID.ToString() + ";#" + currentUser.LoginName.ToString();
...
//called in page_load
 private void GetButtonView()
    {
        try
        {
            bool foundUsers = (from SPListItem item in tn.Items
                               where item["Attendee"].ToString() == userString
                               && item["Event"].ToString() == currentEventItem.Title
                               select item).Count() > 0;
            if (foundUsers)
            {
                ...
                ButtonRegister.Visible = false;
            }
            else
            {
                ...
                ButtonUnregister.Visible = false;
            }
        }
        catch (Exception ex)
        {
            ...
        }
    }
.

问题在此用例中:如果事件标题(“标题”)稍后在事件列表中更改,则无法获得正确的映射与会者=>事件,因为我只检查匹配标题字符串。

首选解决方案:在ATENDEES中制作“事件”列 - 列出查找列,获取实际事件(ID和标题,我认为),并在“与会者”列表中显示“事件”列表中的标题,因此,如果稍后在“事件” - 列表中更改了标题,则与会者列表将自动更新其“事件”-Column中的引用条目,我可以显示“注册”/“取消注册” - 除了具有正确的映射,肯定是必要的;)。

如果有人能给我一个解决方案/提示如何以编程方式做出这个解决方案,那就太好了,因为我没有得到它工作。

最好的问候, Dominik

p.s。我是德国人所以我希望你能理解我的问题。随意询问更多细节!我会尽力以适当的英语解释它。

编辑1:这是我的所以的addattendee-方法,也是:

private void AddAttendee() {
        try
        {
            //Using this because read-permissions are used for attendees
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite ElevatedSite = new SPSite(site.ID))
                {
                    using (SPWeb ElevatedWeb = ElevatedSite.OpenWeb(web.ID))
                    {
                        SPList attendeeList= ElevatedWeb.Lists["Attendees"];
                        SPListItemCollection listItems = attendeeList.Items;
                        SPListItem item = listItems.Add();
                        item["Name"] = currentUser.ID.ToString() + ";#" + 
                        currentUser.LoginName.ToString() + ";#";
                        item["Event"] = currentEventItem["Title"];
                        ElevatedWeb.AllowUnsafeUpdates = true;
                        item.Update();
                        ElevatedWeb.AllowUnsafeUpdates = false;
                        ...
                    }
                }
            });
        }
        catch (Exception ex) 
        {
            ...
        }
    }
.

有帮助吗?

解决方案

As far as I know, changing field type from text to lookup is not supported in Sharepoint.

You should create new lookup field in the Attendees list and point it to Events list, either from Sharepoint list settings, or from code.

Basically, lookup field has the same structure as user field, which you populated in the final bit of your code - ID#;Title. Also, lookup field value is represented by SPFieldLookupValue class, so it can be retrieved like that:

return new SPFieldLookupValue((string)item["EventLookup"]);

You can set lookup field value by assigning new SPFieldLookupValue object to SPListItem property:

item["EventLookup"] = new SPFieldLookupValue(1, "Great Event");

where 1 - Event list item Id and "Great Event" - Event list Item title.

If I also may add some remarks to your code examples. As far as I can see, you are searching attendees for the event by iterating through SPList.Items object and comparing a string value of "Attendee" field with custom string value.

Such a search can return wrong and slow results, because usually user lookup field contains user display name, not a Login Name. I'd recommend using CAML query instead:

SPQuery qry = new SPQuery();
qry.Query =
"  <Where>"
+"    <And>"
+"      <Eq>"
+"          <FieldRef Name='Event' />"
+"          <Value Type='Text'>"+ currentEventItem.Title +"</Value>"
+"      </Eq>"
+"      <Eq>"
+"          <FieldRef Name='Attendee' LookupId='TRUE' />"
+"          <Value Type='Integer'>"+ currentUser.ID.ToString() +"</Value>"
+"      </Eq>"
+"    </And>"
+"  </Where>";
SPListItemCollection listItems = spList.GetItems(qry);
bool foundUsers = listItems.Count > 0;

The query above searches by Event field (type "text" as in your example) and by Attendee user Id without user login Name. Also it retrieves only items, that fall whithin the query where clause.

许可以下: CC-BY-SA归因
scroll top