Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Another day, another question ;)

So, I have two custom Lists, "Attendees" and "Events". In the Events-List there's a Lookupfield "Registrations" which I want to fill programmatically on page_load. In the Events List there's a Lookupfield "Event".

First of all: I know this can be done via UI of SharePoint, and to be honest I just implemented it like this at the moment. But what I really want to achieve is the following:

In the Events-List there are two more columns, lets call them "minimal attendees" and "maximal attendees".

So now I don't just want to show the number in the "Registrations"-Column for an event, I want to show it like this: "1 of 15", ideally via SPHttpEncode in different colors (something like "orange" if registrations < min. Attendees, green if >= min. attendees and < max. attendees and red if registrations == count_related)

Now I don't know how to get the count-related value programmatically (using c#) and hope you could help :)

Best regards, Dominik

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I suppose that you use SPLookupField which multiple property is set to true. In this case you can cast field value to the SPFieldLookupValueCollection and get its Count.

string rawvalue = item[SPBuiltInFieldId.Predecessors].ToString();
SPFieldLookupValueCollection values = new SPFieldLookupValueCollection(rawvalue);
int c = values.Count;
share|improve this answer
    
Hi Alexander and first of all thank you for your answer. To be honest, this seems to be a "bad hack" in my opinion, but I haven't found any other way yet. I'm still searching for one, though. If I won't find one the next 24h or so, I'll check your answer for it is "a" answer, I just think there have to be better ways to achieve my goal. –  Dominik Dec 10 '12 at 11:10
    
@Dominik, you can also look at remark for CountRelated property of lookup field. –  Alexander Dec 10 '12 at 11:21
    
I did it via int registrations = Convert.ToInt32(currEventItem["Registrations"].ToString() now ("Registrations" is the count-related lookup). It is a mess, in my opinion, but apart from the view-issue I need the int value to check if the registration limit has been reached, so... just works ;) Nonetheless you are right with your answer, so thank you. –  Dominik Dec 10 '12 at 13:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.