Friday, March 25, 2011

List all users and groups from SharePoint site collection

Environment: MOSS 2007                  Click here for SharePoint Online Version

The following code is to generate a list on all site collection users, their groups and subsite details

- I've created a custom list named "All Site Users" with  5 columns as User Name, Groups, Site URL, Account Name, TagUsers

- The list template is uploaded programmatically on feature activation, The below code is a webpart code, which is added to a webpart page on solution deployment, when the webpart page is accessed, the following code will loop through the users in the site collection their groups and log the details to the custom list "All Site Users".

-'TagUsers' column can be used to create a view to list unique user names, condition for the view would be Filter by TagUsers=1


SPWeb rootweb = null;
                SPSite SPSite = null;
                try
                {
                   using (SPSite = new SPSite(SPContext.Current.Site.ID))
                    {
                        using (rootweb = SPSite.OpenWeb())
                        {
                            //Delete and create a new Portal Users List with updates
                            DeleteListIfExists(rootweb, "Portal Users List");
                            SPListTemplateCollection templateCollection = SPSite.GetCustomListTemplates(rootweb);
                            SPListTemplate template = templateCollection["Portal Users List"];
                            rootweb.AllowUnsafeUpdates = true;
                            //Create the Portal Users List using the PortalUsersList.stp template,                            rootweb.Lists.Add("Portal Users List", "List to display all the site collection users and their groups", template);
                            //Getting the URL of the list
                            SPList alluserslist = rootweb.Lists["Portal Users List"];
                            string ListURL = alluserslist.DefaultViewUrl;
                            Hashtable htUsers = new Hashtable();
                            //Loop all the Sites
                            foreach (SPWeb SPWeb in SPSite.AllWebs)
                            {
                                //Get All users in the subsite
                                SPUserCollection AllSPWebUsers = SPWeb.AllUsers;
                                //Loop each users in the Web/subsite
                                foreach (SPUser user in AllSPWebUsers)
                                {
                                    //Adding the user to the "Portal Users List" list
                                    SPListItem newItem = alluserslist.Items.Add();
                                    SPGroupCollection AllGroups = user.Groups;
                                    //AllGroups.Count is '0' when the users are directly added to the site
                                    if (AllGroups.Count == 0)
                                    {
                                        newItem["User Name"] = user.Name;
                                        newItem["Account Name"] = user.LoginName;
                                        newItem["Parent Site"] = SPWeb.Url;
                                        newItem["Group"] = "Individual User";
                                       
                                        if (!htUsers.Contains(user.LoginName))
                                        {
                                            newItem["TagUsers"] = 1;
                                            htUsers.Add(user.LoginName,user.Name);
                                        }
                                       
                                    }
                                    else
                                    {
                                        //Loop each group the user added to
                                        foreach (SPGroup group in AllGroups)
                                        {
                                            newItem["User Name"] = user.Name;
                                            newItem["Account Name"] = user.LoginName;
                                            newItem["Parent Site"] = SPWeb.Url;
                                            newItem["Group"] = group.Name;
                                            if (!htUsers.Contains(user.LoginName))
                                            {
                                                newItem["TagUsers"] = 1;
                                                htUsers.Add(user.LoginName, user.Name);
                                            }
                                        }
                                    }
                                    //Saving new item in the list
                                    newItem.Update();
                                }
                                alluserslist.Update();
                            }
                            //After WebPart process the entire users list, will be redirected to the "Portal Users List" page
                            SPUtility.Redirect(ListURL, SPRedirectFlags.Trusted, HttpContext.Current);
                        }
                    }
                }
                catch (ThreadAbortException)
                {
                    //Ignoring this as SPUtility.Redirect throws ThreadAbortException
                }
                   catch (Exception e)
                {
                 }
             }

Wednesday, March 9, 2011

InfoPath URL in Navigation

If you need to provide a navigation menu in SharePoint page with the InfoPath Form URL, your URL will be truncated if it is length exceeds 256 characters, to shorted the URL we can use


~sitecollection in the URL instead of "http://servername/site"


The full URL after change would be similar like below


https://servername/sites/sitename/_layouts/FormServer.aspx?XsnLocation=~sitecollection/sitename/InfoPathForms/rrtemplate.xsn&SaveLocation=~sitecollection/sitename/FormLibName&Source=~sitecollection/sitename/FormLibName/Forms/AllItems.aspx&DefaultItemOpen=1

Wednesday, March 2, 2011

URL properties used in SharePoint designer

Assume that https://server/sites/sitename/libraryname/formname.xml
  the value for the URL properties would be

Server Relative URL: /sites/libraryname/formname.xml
URL Path: /sites/sitename/libraryname/formname.xml

Wednesday, February 23, 2011

Tuesday, February 22, 2011

Publish InfoPath 2007 form in MOSS 2007

1. Design the form
2. Create a form library to store the source form template (xsn).
3. Create another form library to store all the submitted forms
4. Submit data to document library(step 2) on Submit option
5. Publish form to the form library (step 1)

Monday, February 21, 2011

Table border color always black in InfoPath table

To change the border color of a table in InfoPath

1. Right-click the table that you want to change the color.
2. Click Borders and Shading menu.
3. In Borders Tab, select the color that you want to apply for the table border from color palette.
4. Under Presets, click the button for the borders that you want. (NOTE: you need to click the preset button every time you change the color, otherwise the border color remains unchanged)
5. Click OK

Friday, February 18, 2011

Add Label Control in InfoPath

To add a dynamic label control in InfoPath 2007, we can use the Expression Box in Advanced Controls section.

For example, if you want to insert the current date as a dynamic label in the form,


1. Add Expression Box in the form
2. Insert function Date and Time --> Today
3. To change the Date format - Set the format under Result Section in General Tab of Expression Box Properties