Search This Blog

Sunday, October 12, 2008

Increase Size of Site Template using STSADM

To increase the size of the default sharepoint template size for Sites

 Run Command Prompt, Type in

stsadm -o setproperty -pn max-template-site-size -pv 200000000

The line above will change the max size that can be saved as a site template to 20MB, the default value that is set is ony 10MB.

Wednesday, October 1, 2008

Global variables in Javascript

L_Menu_BaseUrl - the base URL of the site.

L_Menu_LCID - the LCID of the site you're in. Useful if you need to determine the language of the site.

.
L_Menu_SiteTheme - the name of the theme applied to the site.

There is one more useful variable, but this one can't be used on custom master pages that you created. This one is used in the SharePoint's default pages:

_spUserId - the ID of the logged in user

Saturday, July 12, 2008

Display site members from AD Groups

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() + "
");}}}}

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;}

Tuesday, March 25, 2008

Display Current User Name in a page

I had to display the current user name in a page within Sharepoint, So I created this javascript to get the name from the welcome link.

1. Enter the javascript within the page.aspx

<!– Script for displaying name –>

<script language=”javascript” type=”text/javascript”>
var Loginname = document.getElementById(“zz6_Menu”).innerHTML ;
var end = Loginname.indexOf(“<”);
var nameOnly = Loginname.substring(8, end);
document.write(nameOnly);
</script>

There are 2 ways which you can get the Id which is highlighted in red.

1. Right the page and view source to get the ID

2. Using IE Developer Toolbar, click on the Name

welcome.png

XPath Operators

XPath Queries


/ (child operator)

Refers to the root of the XML document when used at the beginning of the XPath expression. The child operator is used to specify the next child to select. The expression "/Employees/Employee", for, example says, start at the root of the XML document, select the Employees node and then select all the Employee child nodes within the Employees node. This will return the two Employee nodes in the sample XML document.

// (recursive descendant operator)

The recursive descendant operator indicates to include all descendant nodes in the search. Using the operator at the beginning of the XPath expression means you start from the root of the XML document. The expression "//LastName" starts at the root and finds any LastName node. The expression "/Employees//LastName" selects the Employees node and then, within that node, finds any LastName node. It yields the same result, but searches in a different way.

* (wildcard operator)

The wildcard operator finds any node. The expression "/*" finds any node under the root, which in our case is Employees. The expression "/Employees/*" means find any node under the Employees node, which in our case results with the two Employee nodes. Now what is the difference between the "/Employees" and "/Employees/*" expression? The first expression returns the Employees node but the second node finds any node under the Employees node, meaning it returns the two Employee nodes. The expression "//*" means to select any node including descendant nodes, so it will effectively list every single node in the complete XML document.

. (current context operator)

The current context operator refers to the current context. For example, you have written some code that selected the Employees node and then from there you run the expression "./Employee", which means it starts out from the currently selected Employees node and then selects the two Employee nodes. The expression "Employee" would yield the same result because it also starts out from the current context. Similar the expression ".//LastName" means start from the current context, the Employees node, and find any LastName node including any descendant nodes.

.. (parent operator)

The parent operator refers to the parent. For example, the expression "/Employees/Employee/.." returns the Employees node because you navigate down to the Employee nodes and then tell it to return its parent, which is the Employees node.

@ (attribute operator)

The attribute operator refers to an attribute instead of an element. The expression "/Employees//@ID" selects any ID attribute it finds under the Employees node. Now, keep in mind that the XPath query always returns the selected node. In the case of an attribute, the node below it is its value. So, the expression really two returns nodes, each with the value of each selected attribute. Furthermore, you can use the wildcard operator with attributes, so "/Employees//@*" means any attribute underneath the Employees node.

[ ] (filter operator)

You can apply a filter operator to filter the selected nodes. This works with attributes and with elements. The expression "/Employees/Employee[@ID=1]" returns any Employee node under the Employees node that has an ID attribute with the value one. You also can apply filters that just say that an attribute or element with that name needs to be present. For example, the expression "/Employees/Employee[WebAddress]" returns Employee nodes that have a WebAddress node as child. The expression "/Employees/Employee[FirstName='Klaus']" returns the Employee node that has a FirstName node with the value Klaus.

text() function

The "text()" function refers to the text of the selected node or attribute. The expression "//Employee//text()" does not list all the descendant nodes of all Employee nodes but rather the value for each descendant node. The expression "//Employee/FirstName[text()='Klaus']" lists all FirstName nodes which have a value of Klaus.

[ ] (collection operator)

When your expression returns more then one node with the same name, you have a collection returned. The expression "//Employee" returns two Employee nodes, which is nothing more than a collection of Employee nodes. You can apply a collection operator and specify which item from the collection you want to select. Keep in mind that the index starts at one. The expression "//Employee[2]" returns the second Employee node. The order of the selected nodes is the same order as in the XML document. You can use the collection operator in any blend, such as "//Employee[1]/LastName", which selects the first Employee node and then from there the LastName node.

( ) (group operator)

The collection operator can sometimes have some odd side effects. Assume you have two Employee nodes and each has two Job nodes. What does the expression "//Employee/Job[1]" return? It returns the first Job node for each selected Employee node. But, using the group operator allows you to apply explicit precedence to selections. The expression "(//Employee/Job)[4]" first selects all Job nodes for all Employee nodes and from that collection it returns the fourth node. The group operator can only be applied to the top level expression; for example, "//Employees/(Employee/FirstName)" is invalid.

comment() function

Returns a comment node. The expression "//comment()" returns any comment node in the XML. The expression "/Employees/comment()" returns the comment nodes under the Employees node.

node() function

XML documents consist of elements, attributes, and their values, each being a node. So, in XPath expressions you can use a node() function instead of a node name or the text() function. It is a generic way to address a node. The expressions "//Employee/JobTitle/node()" and "//Employee/JobTitle/text()" return the same result, the value of both JobTitle nodes. But, "//Employee//node()" will not just return the elements but also the values of each element, because both are nodes.
| (union or set operator)

Returns the union of one or more location paths. The expression "//LastName
//FirstName" returns all the LastName and FirstName nodes. It preserves the order of the elements as in the XML and does not return any duplicates. The two location paths "//Employee[@ID=1]
//Employee[FirstName='Klaus']" return the same nodes but the union of these two returns just the one unique node.

Customizing RSS Viewer to display Weather.com forecast

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>


Get User Collection from a Sharepoint Group

I have been working on getting a drop down list in Infopath to be populated with users of a Sharepoint Group. You can't connect directly to the drop drop list, so you will need to create 2 data sources, 1 to the web service and another xml file with dummy data which will be replace be information from the web service.
1. Create a data connection to the web service using the data connection wizard.
<http://servername/_vti_bin/UserGroup.asmx?WSDL>
Data Connection


2. Click next and select GetUserCollectionFromGroup
3. Enter Sample data, which is the name of the group which you wish to get data from.
4. You will need dummy data to be connected to the drop down list first. There are 2 dummy users in the xml, it tells infopath that it is repeating.

DummyData.xml

<?xml version=”1.0″ encoding=”utf-8″ ?>

<dfs:myFields xmlns:dfs=”http://schemas.microsoft.com/office/infopath/2003/dataFormSolution” xmlns:s0=”http://schemas.microsoft.com/sharepoint/soap/directory/” xmlns:my=”http://schemas.microsoft.com/office/infopath/2003/myXSD/2004-03-16T06-17-54″ xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema”> <dfs:queryFields> <s0:GetUserCollectionFromGroup> </s0:GetUserCollectionFromGroup> </dfs:queryFields> <dfs:dataFields> <GetUserCollectionFromGroupResponse xmlns=”http://schemas.microsoft.com/sharepoint/soap/directory/”> <GetUserCollectionFromGroupResult> <GetUserCollectionFromGroup> <Users> <User ID=”1″ Sid=”S-1-5-21-3593225548-2099380924-3969701235-500″ Name=”User1″ LoginName=”login1″ Email=”user1@users.com” Notes=”" IsSiteAdmin=”False” IsDomainGroup=”False”/> <User ID=”2″ Sid=”S-1-5-21-3593225548-2099380924-3969701235-1145″ Name=”User2″ LoginName=”login2″ Email=”user2@users.com” Notes=”" IsSiteAdmin=”False” IsDomainGroup=”False”/> </Users> </GetUserCollectionFromGroup> </GetUserCollectionFromGroupResult> </GetUserCollectionFromGroupResponse> </dfs:dataFields></dfs:myFields>
5. Save the dummydata.xml and connect it via the Data Connection Wizard as well.

6. Link the drop down list to the dummydata.xml
dropdownlist.png


7. You will need to go to the loading event of the infopath form, which you can get to by going to Tools>Programming>Loading Event.
It will open up Visual studio. Within the Loading Event, you will need the following piece of code.

public void FormEvents_Loading(object sender, LoadingEventArgs e){
//Dummy Data
//get an XPathNavigator to our webservice method
XPathNavigator dutyManagerDummy = this.DataSources["DutyManagerListDummyData"].CreateNavigator();
//create the XmlNamespaceManager that will allow us to navigate the xml
XmlNamespaceManager umanager = new XmlNamespaceManager(dutyManagerDummy.NameTable);
umanager.AddNamespace("dfs", http://schemas.microsoft.com/office/infopath/2003/dataFormSolution);
umanager.AddNamespace("tns", http://schemas.microsoft.com/sharepoint/soap/directory/);

//get the node that contains all the users from dummy data

XPathNavigator DutyManagerListDummyDataName = dutyManagerDummy.SelectSingleNode("/dfs:myFields/dfs:dataFields/tns:GetUserCollectionFromGroupResponse/tns:GetUserCollectionFromGroupResult/tns:GetUserCollectionFromGroup/tns:Users", umanager);
//Actual Data
//get an XPathNavigator to our webservice method
XPathNavigator dutyManager = this.DataSources["GetUserCollectionFromGroup"].CreateNavigator();
//create the XmlNamespaceManager that will allow us to navigate the xmlXmlNamespaceManager dmanager = new XmlNamespaceManager(dutyManager.NameTable);
dmanager.AddNamespace("dfs", http://schemas.microsoft.com/office/infopath/2003/dataFormSolution);
dmanager.AddNamespace("tns", http://schemas.microsoft.com/sharepoint/soap/directory/);
//get the node that contains all the users from the call to the web service
XPathNavigator dusers = dutyManager.SelectSingleNode("/dfs:myFields/dfs:dataFields/tns:GetUserCollectionFromGroupResponse/tns:GetUserCollectionFromGroupResult/tns:GetUserCollectionFromGroup/tns:Users", umanager);
try{
//replace the dummy information from the information from the web service
DutyManagerListDummyDataName.ReplaceSelf(dusers); }
catch(NullReferenceException ee){}
}
Run the preview and you should see the actual data instead of the dummy information.

Sunday, March 2, 2008

Customizing Content Query Webpart

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

InternalName, DateField

Example: Calendar Properties
EventDate,DateTime; Description,Note; Location,Text; Property_x0020_Unit,Choice

  •  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 tag.