문제

내 유스 케이스 :

비주얼 웹 파트를 사용하여 SharePoint에 이벤트 관리 시스템을 구현하려고합니다.

두 개의 사용자 정의 목록이 있으며, 하나는 "이벤트"라고합니다. DisplayForm (사실 출구 밖의 타이틀 필드, 실제로) 및 다른 열에있는 다른 비 관련 세부 정보를 보여주는 "제목"열이있는 열이있는 열 "제목"이라고 가정 해 봅시다.

두 번째 목록을 "참석자"라고합니다. 첫 번째 열이 두 개의 열이 있습니다. "참석자"(현재 사용자의 loginName을 가져 오는 사용자 필드). 둘째 : "이벤트"(순간 : 이벤트 제목 문자열)

내 시각적 인 WebPart에서 ( "이벤트"-List의 디스플레이 양식 위의 Via & ToolPaneView= 2가 추가됨) 다음 코드에 따라 버튼 ( "레지스터"및 "등록되지 않은")을 표시합니다.

...
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 및 제목, ID와 제목)를 가져 오는 조회 컬럼을 나열하고 "참석자"목록 "이벤트"목록의 제목을 표시합니다. 따라서 "이벤트"-List에서 제목이 나중에 변경되면 참석자 목록은 "이벤트"-Column에서 참조 항목을 자동으로 업데이트하고 "레지스터"/ "등록되지 않은"- 부트턴을 표시 할 수 있습니다. 확실히 필요한 올바른 맵핑이 필요합니다.).

누군가가 나에게 해결책을 줄 수 있다면 좋을 것입니다.

최고의 안부, Dominik

p.s. 나는 독일이므로 당신이 내 질문을 이해하기를 바랍니다. 자세한 내용은 자유롭게 물어보십시오! 나는 적절한 영어로 설명하기 위해 최선을 다할 것입니다.

편집 1 : 여기에 내게 addattendee-method,

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 ~와 함께 속성
제휴하지 않습니다 sharepoint.stackexchange
scroll top