Sharing SharePoint

Always a rookie in the SharePoint World

Archive for the ‘WebParts’ Category

Display site members from AD Groups

Posted by Jessica Wang on July 13, 2008

I recently have to do this for a client. The web part is suppose to display a list of all users from the Sharepoint Security groups for every site. The out of the box webpart(Site Users) will display only the name of the AD group and not the members within it.

To get started, it is easier if you installed the Webpart templates for VS.NET

Created a solution using webpart template and you are ready to go.

Call this function to pass in the name of the ADgroup and it will return a list of users name.

//Query Active Directory to get users from Active Directory Groups

public StringCollection GetGroupMembers(stringstrGroup)

{StringCollection groupMemebers = new StringCollection(); 

try

{DirectoryEntry ent = new DirectoryEntry(LDAP://OU=youOU,DC=yourDC);

DirectorySearcher srch = new DirectorySearcher(“(CN=” + strGroup + “)”);

SearchResultCollection coll = srch.FindAll(); 

foreach (SearchResult rs in coll)

{ResultPropertyCollection resultPropColl = rs.Properties;

foreach (Object memberColl in resultPropColl[“member”])

{DirectoryEntry gpMemberEntry = new DirectoryEntry(“LDAP://”+ memberColl);

System.DirectoryServices.PropertyCollection userProps = gpMemberEntry.Properties;

//getting user properties from AD

object obVal = userProps[“displayName”].Value;

object obAcc = userProps[“sAMAccountName”].Value;

if (null != obVal) {

 groupMemebers.Add( “User Name:” +obAcc.ToString() + “, User login name:” + obVal.ToString() + “<br>”);}}}}

catch (Exception ex)

{//writer.Write(ex.Message);}

Return groupMemebers;

To get the names of the current site users from Sharepoint Security Groups:

ArrayList belongToDomain = new ArrayList();

ArrayList names = new ArrayList();

using(SPSite collSite = new SPSite(SPContext.Current.Site.ID))

{using (SPWeb elevatedWeb = collSite.OpenWeb(SPContext.Current.Web.ID))

{//All users in the site

SPUserCollection collUser = SPContext.Current.Web.AllUsers;

SPGroupCollection collgroup = SPContext.Current.Web.Groups;

//for each item in the collection of groups

foreach (object group in collgroup){

//display all users other then the visitors

if (group.ToString() != “Visitors”){

//check that the users in the whole site collection belongs to current site group

foreach (SPUser singleuser in collUser)

{//get the list of groups that the user belongs to

foreach (SPGroup userGroup in singleuser.Groups)

{//check if it matches any of the current site groups

if (group.ToString() == userGroup.ToString())

{//check if the user from the sharepoint group is a AD group

if (singleuser.IsDomainGroup)

{//pass the name into Array that query the AD

belongToDomain.Add(singleuser.ToString());}

 else{

//otherwise add into the Array that stores list of names, in case the user name is not from an AD group.

 names.Add(singleuser.LoginName);

);}} }}}}}}

Now that we have the names of the AD groups from the share point security groups and query the AD for a list of user name. It is now in the names array, we need to make sure that there are no duplicate names. So call the function below and pass in the names array.

//remove duplicate users name Function

public ArrayList RemoveDups(ArrayList items)

{

 ArrayList noDups = new ArrayList();

 foreach (string strItem in items)

    {

        if (!noDups.Contains(strItem.Trim()))

        {

           noDups.Add(strItem.Trim());

        }

    }

    noDups.Sort();

    return noDups;}

Posted in WebParts | Tagged: , , , , | 10 Comments »

Customizing RSS Viewer to display Weather.com forecast

Posted by Jessica Wang on March 25, 2008

Step 1:  Register for Free at Weather.com to get your Partner ID and License Key

Step 2: Enter http://xoap.weather.com/weather/local/ASXX0075?cc=*&dayf=6&par=PartnerID&prod=xoap&unit=m&key=LicenseKey into RSS Feed URL

Step 3: Edit the XSL

<?xml version=”1.0″ encoding=”ISO-8859-1″ ?><xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”><xsl:output method=”html” indent=”yes” encoding=”iso-8859-1″ /><xsl:template match=”/weather”>

<xsl:variable name=”med-img-dir”>WeatherIcons/60×59</xsl:variable>

<!–Purpose: Weather Webpart, information from Weather.com–>

         <xsl:variable name=”day” select=”dayf/day[@d]”></xsl:variable>

        <xsl:for-each select=”$day”>

 <!– Current Weather Conditions –>

<table width=”100%”>

                <tr>

                                <!–Image–>

                                <td style=”width: 70px;font-size: 11px” rowspan=”3″>

                                <xsl:variable name=”img-ext”>png</xsl:variable>

                                <xsl:variable name=”iconnumber” select=”part[@p=’d’]/icon” />

                                <img src=”{$med-img-dir}/{$iconnumber}.{$img-ext}” mce_src=”{$med-img-dir}/{$iconnumber}.{$img-ext}” alt=”{$iconnumber}.{$img-ext}” width=”40″ height=”42″ />

                                </td>

                                <!–Day and Date–>

                                <td style=”font-size: 11px; font-weight: bold; padding:0px 0px 0px 0px” colspan=”2″>

                                                                <xsl:value-of select=”./@dt” />,

                                                                <xsl:value-of select=”./@t” />

                                                                </td>

                </tr>

                <tr>

                                <td style=”font-size: 11px; padding:0px 0px 0px 0px” colspan=”2″>                                        

                                                                <xsl:value-of select=”part[@p=’d’]/t”/>

                                                                </td>

                </tr>

                <tr>

                <!–Temperature–>

                                                                <td style=”font-size: 11px; padding:0px 0px 0px 0px”>Min:

                                                                <xsl:value-of select=”low”></xsl:value-of>C </td>

                                                                <td style=”font-size: 11px; padding:0px 0px 0px 0px” >Max:

                                                                <xsl:value-of select=”hi”></xsl:value-of>C </td>

                </tr>

</table>

</xsl:for-each>

</xsl:template>

</xsl:stylesheet>

Posted in WebParts, XSLT | Leave a Comment »

Customizing Content Query Webpart

Posted by Jessica Wang on March 3, 2008

  

Firstly, Heather Solomon has an excellent article on it.

Part 1: Export Content Query Web part

Open it up and search for CommonViewFields

Add in Fields that you want; you will need to use the internal name and the type of data.

A good tool is Sharepoint Explorer for WSS 

<property name=”CommonViewFields” type=”string”>InternalName, DateField</property>

Example: Calendar Properties 

<property name=”CommonViewFields” type=”string”>EventDate,DateTime; Description,Note; Location,Text; Property_x0020_Unit,Choice</property>

Save the Web Part and Import it back 

Part 2: Locate ItemStyle.xsl under XSL Style Sheet

Create your own custom Item Style 

<xsl:template name=”CustomStyle” match=”Row[@Style=’CustomStyle’]” mode=”itemstyle”>  <xsl:variable name=”SafeImageUrl”>
            <xsl:call-template name=”OuterTemplate.GetSafeStaticUrl”>
                <xsl:with-param name=”UrlColumnName” select=”‘ImageUrl'”/>
            </xsl:call-template>
        </xsl:variable>
  <xsl:variable name=”SafeLinkUrl”>
            <xsl:call-template name=”OuterTemplate.GetSafeLink”>
                <xsl:with-param name=”UrlColumnName” select=”‘LinkUrl'”/>
            </xsl:call-template>
        </xsl:variable>
  <xsl:variable name=”DisplayTitle”>
            <xsl:call-template name=”OuterTemplate.GetTitle”>
                <xsl:with-param name=”Title” select=”@Title”/>
                <xsl:with-param name=”UrlColumnName” select=”‘LinkUrl'”/>
            </xsl:call-template>
        </xsl:variable>
  <xsl:variable name=”LinkTarget”>
            <xsl:if test=”@OpenInNewWindow = ‘True'” >_blank</xsl:if>
        </xsl:variable>
  <table>
   <tr>
    <td style=”width: 200px”>          
  <xsl:value-of select=”ddwrt:FormatDate(string(@EventDate),3081, 3)”/></td>
    <td valign=”top” rowspan=”3″>|</td>    <td class=”linkitem” style=”width: 200px”>      
    <xsl:call-template name=”OuterTemplate.CallPresenceStatusIconTemplate”/>
                <a href=”{$SafeLinkUrl}” mce_href=”{$SafeLinkUrl}” target=”{$LinkTarget}” title=”mailto:%7B@LinkToolTip}”>
                    <xsl:value-of select=”$DisplayTitle”/>
                </a>
        </td>
   </tr>
   <tr>
    <td rowspan=”3″ align=”left”>    
    <xsl:value-of select=”ddwrt:FormatDate(string(@EventDate),3081, 4)”/></td>
    <td>Location: <xsl:value-of select=”@Location”/></td>
   </tr>
   <tr>
    <td >PropertyUnit: <xsl:value-of select=”@Property_x005F_x0020_Unit “/>
</td>
   </tr>
   <tr>
    <td> xsl:value-of select=”@Description” disable-output-escaping=”yes”/></td>
   </tr>
    </table>To see what fields are being passed in<xsl:for-each select=”@*”>
    P:<xsl:value-of select=”name()” />
</xsl:for-each>
 </xsl:template>

If you want to format the Date Time using ddwrt, remember to add   xmlns:ddwrt=”http://schemas.microsoft.com/WebParts/v2/DataView/runtime” within the <xsl:stylesheet> tag.
  

Posted in WebParts | Leave a Comment »