Domanda

Il mio caso d'uso:

Sto cercando di implementare un sistema di gestione degli eventi su SharePoint utilizzando le parti Web visive.

Ho due liste personalizzate, si chiama "eventi". Diciamo che ha una colonna "Titolo" che mostra i dettagli dell'evento in un displayForm (il campo del titolo out-of-the-box è, in realtà) e alcuni altri dettagli non rilevanti in altre colonne.

La seconda lista è chiamata "partecipanti". Ci sono due colonne, prima: "Partecio" (campo utente che ottiene il loginName dell'utente corrente). Secondo: "Evento" (al momento: Stringa del titolo dell'evento).

Ay Visual WebPart (aggiunto VIA & TOOLPANEVIEW= 2 Sopra il modulo di visualizzazione degli "Eventi" -List) I Mostra i pulsanti ("Registrati" e "Ungere") A seconda del seguente codice:

...
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)
        {
            ...
        }
    }
.

Problema in questo caso: se il titolo dell'evento ("Titolo") viene modificato in seguito nell'elenco degli eventi, non ottengo un corretto partecipatore di mappatura=> Evento perché al momento controllo solo per la corrispondenza di stringhe del titolo

Soluzione preferita: crea la colonna "evento" all'elenco di Atendees una colonna di ricerca che ottiene l'evento effettivo (ID e il titolo, penso) e mostra il titolo nel "Evento" di "Event" dei "partecipanti", Quindi, se il titolo è cambiato in seguito nell'elenco degli "eventi", l'elenco dei partecipanti aggiornerà automaticamente le voci di riferimento nel suo "evento" -Column e posso mostrare il "Register" / "Ungere" -Button (oltre a avere una mappatura corretta, che è necessaria di sicuro;)).

Sarebbe bello se qualcuno potesse darmi una soluzione / suggerimento come fare questo programmaticamente per non farlo funzionare.

Cordiali saluti, Dominik

P.S. Sono tedesco quindi spero che tu capisca la mia domanda. Sentiti libero di chiedere maggiori dettagli! Farò del mio meglio per spiegarlo in inglese adeguato.

Modifica 1: Ecco il mio metodo di so-Far AdditDendeee, anche:

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) 
        {
            ...
        }
    }
.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top