Peter Sonander

Hi

I'm developing a team site for soccer team. I would like to show a random player (member of the players group) in a webpart on the startpage. Anyone with a sample how to handle this.

Regards

Peter



Re: SharePoint - Development and Programming Show random user in webpart

Ishai Sagi

Are the "players" users in the site, or is this just a list of players

If it's a list, I suggest the following psuedo code:

  1. Connect to the list (see my common and simple tasks - http://www.sharepoint-tips.com/2006/08/common-and-simple-coding-tasks-in.html)
  2. Get the number of items in the list (List.Items.Count)
  3. Generate a random number up to the count, and get the list item in that index.
  4. Get the properties of the list item (again, see my common coding tasks) and display them on the page.

I assume you know how to write a webpart. If not, I suggest you read about this http://blogs.tamtam.nl/mart/CreateASharePoint2007WebpartStepByStep.aspx






Re: SharePoint - Development and Programming Show random user in webpart

Peter Sonander

Thanx

They are users in the site, do you know if it possible to handle that instead

Otherwise thanks for the sample





Re: SharePoint - Development and Programming Show random user in webpart

Ishai Sagi

Peter,

In my blog (link below) you will find (search for "common coding tasks") how to connect to the SPWeb object using code.

After doing that, you have access (using the .Users property of the SPWeb object) to the list of users that:

  • have access to the site
  • and have been added explicitly (not as part of a group)
  • or have been added through a group and have visited the site.

You can then implement the same logic I mentioned earlier with randomly selecting one of them (use the SPUser object to get to their properties as are tracked in the site)






Re: SharePoint - Development and Programming Show random user in webpart

Peter Sonander

Thanks

I'll try that.

Peter





Re: SharePoint - Development and Programming Show random user in webpart

rick postmus

I had a situation where i wanted to select random records out of a list. I used a dataviewwebpart to get the data and edited the xsl to get random results.

Your steps actually helped me with this proces.

I did the following:

1. create a dataviewwebpart

2. connect to a list

3. make a variable that counts the rows

4. make a variable that does select="ddwrt:Random(1,$CountRows)"

5. then give the FirstRow and LastRow the select value of the variable name in step 4.

This worked for me without making an whole new webpart

Greetz





Re: SharePoint - Development and Programming Show random user in webpart

ShareMyPoint

Thanks for your post Rick it was exactly what i was looking for. However as I am a newby to this I am getting stuck with your instructions. I have created a daatview web part that displays a single item form from the list. I am not to clear on:

1) Do I need to create a variable to count rows when there is already one there:

<xsl:variable name="dvt_RowCount" select="count($Rows)" />

I have added a variable to select the random item:

<xsl:variable name="randomtip" select="ddwrt:Random(1,$dvt_RowCount)" />

I have also changed the select value of the first and last rows:

<xsl:with-param name="FirstRow" select="randomtip" />
<xsl:with-param name="LastRow" select="randomtip" />

I only get the same item each time, which is the first item in the list.

Any help would be much appreciated.

Daniel





Re: SharePoint - Development and Programming Show random user in webpart

rick postmus

Im no programmer myself but i think the following:
Code Block

<xsl:with-param name="FirstRow" select="randomtip" />

<xsl:with-param name="LastRow" select="randomtip" />

Should be:
Code Block

<xsl:with-param name="FirstRow" select="$randomtip" />

<xsl:with-param name="LastRow" select="$randomtip" />