Thursday, March 31, 2011

Move SharePoint Site Collection as a Subsite

Environment: SharePoint 2007/2010/2013

Requirement: To move a site collection as a subsite to another site collection, we can make use of STSADM or Powershell import/export command.

Following are the steps to achieve this

Assume that, we need to move the site collection http://sitecollection1/sites/asite to another site collection http://sitecollection2/sites/bsite as a subsite.


Steps

1. Export the site collection (http://sitecollection1/sites/asite) using STSADM command line tool

using stsadm command
stsadm.exe -o export -url http://sitecollection1/sites/asite -filename c:\backups\sc_asite.bak –includeusersecurity –nofilecompression

using powershell
Export-SPWeb -Identity http://sitecollection1/sites/asite -Path C:\backups\sc_asite.bak -IncludeUserSecurity -NoFileCompression

2. Create a new site with Blank Site template in the other site collection (http://sitecollection2/sites/bsite). Assume the blank site name is csite

3. Import the backed up site collection in c:\mybckup\sc_asite.bak

using stsadm command
stsadm.exe -o import -url http://sitecollection2/sites/bsite/csite -filename c:\backups\sc_asite.bak 
-includeusersecurity -nofilecompression

using powershell
Import-SPWeb http://sitecollection2/sites/bsite/csite  -Path c:\backups\sc_asite.bak -IncludeUserSecurity -NoFileCompression


After migration, the site collection 1 will be available under the following URL as a subsite http://sitecollection2/sites/bsite/csite



Tuesday, March 29, 2011

Unable to cast object of type 'XXXXXXXX' to type 'Microsoft.SharePoint.SPFeatureReceiver'

I got the following error on deployment
"Unable to cast object of type  XXXX.YYYYY' to type 'Microsoft.SharePoint.SPFeatureReceiver'

Resolution:
Removed the following entries ReceiverAssembly & ReceiverClass from feature.xml and the issue is resolved

ReceiverAssembly="XXXX.YYYYY' , Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5"
ReceiverClass="XXXX.YYYYY.ZZZZZ"

Friday, March 25, 2011

To provision a file in Pages document library

Below is the feature file that is used to upload a file in to Pages document library on feature activation

Feature.xml

<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="21a53317-2fd7-4f21-b5c4-efc018b88154"
          Title="Add Site Users List Template "
          Description="Upload the 'All Site Users' list template to site collection"
          Version="12.0.0.0"
          Hidden="FALSE"
          Scope="Site"
    ActivateOnDefault="FALSE"
          DefaultResourceFile="core"
          ReceiverAssembly="xxxx.yyy.ListAllUsers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5"
          ReceiverClass="xxxx.yyy.ListAllUsers.AddGetUsersList"       
          xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="elements.xml"/>
   <ElementFile Location="webpartpage.aspx"/>
  </ElementManifests>
</Feature>

Elements.xml

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="Pages" Path="" Url="Pages">
<File Name="webpartpage.aspx" Url="webpartpage.aspx" IgnoreIfAlreadyExists="FALSE"/>
</Module>
</Elements>

Once the feature is activated the file is uploaded to the document library, however I cannot view the file in default view but able to view in the Explorer view, currently working on that

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