Friday, October 28, 2011

How to change the view used by XsltListViewWebPart

So I was in a situation that I needed to change what view was being displayed on a page dynamically.
I am using the the XsltListViewWebPart to display all the values of a list to the user and depending on the user and different properties of the list items I needed to filter the view as well as assign a different XSL file.

The biggest problem I had was that the list view GUID  is created only at deploy time and I could find no other way to assign a different view.

I came across several posts that had examples of changing the view or adding a new XLVWP to the page at deploy time
http://sharepoint.stackexchange.com/questions/4843/reusing-the-xsltlistviewwebpart and http://social.technet.microsoft.com/Forums/zh/sharepoint2010programming/thread/983d2098-d742-4708-8bdc-437aa07b3b9d

So I wanted to take this a little further and be able to dictate what view was used on the fly.  Using a QueryString of the ListID and the ListViewName I then assign the appropriate view in the OnInit event as shown below.


public partial class RecordHistory : DialogLayoutsPageBase
{
     protected void Page_Load(object sender, EventArgs e)
     {
     }
     protected override void OnInit(EventArgs e)
     {
         base.OnInit(e);
         string[] strLists = Request.QueryString.GetValues("ListID");
         string[] strListViewName = Request.QueryString.GetValues("ListViewName");

         if (strLists == null || strLists.Length < 1)
         {
             throw new SPException(SPResource.GetString(Strings.MissingRequiredQueryString, "ListID"));
         }
         SPList spList = SPControl.GetContextWeb(Context).Lists.GetList(new Guid(strLists[0]), true);
         SPView view = spList.Views[strListViewName[0]];
         this.xlvwpRecordHistory.ViewGuid = view.ID.ToString();
         this.xlvwpRecordHistory.ViewId = int.Parse(view.BaseViewID);
         this.xlvwpRecordHistory.XmlDefinition = view.GetViewXml();
     }
}

As always there are many ways to do this that might be better but this worked and I found no other examples of this on the web.
Please let me know if you have a better idea

Monday, June 6, 2011

Basic steps for Moving a SharePoint site collection between domains

Before you start
Make sure SharePoint versions are the same on source and destination servers, service packs and patches.

Permissions
There are some basic permissions that need to be configured (if they have not already).
See Add-SPShellAdmin. If you are using Windows authentication to connect to SQL Server, the user account must also be a member the SQL Server dbcreator fixed server role on the SQL Server instance where the database will be created. If you are using SQL authentication to connect to SQL Server, the SQL authentication account that you specify when you create the content database must have dbcreator permission on the SQL Server instance where the database will be created.

Add-SPShellAdmin -UserName Domain\User Name

Migrate Data
Backup Site Collection on Farm A.
On the server farm that contains the original site collection open a SharePoint 2010 Management Shell.
In the power shell window type the following command:
Backup-SPSite –Identity http://FarmASiteCollection -Path TempPathAndFileName.bak -Force
Force will overwrite a previously backup file.

Create Content Database on Farm B.
Assuming the appropriate Web Application already exists then you just need to create a new content database.
On the server for farm B open a SharePoint 2010 Management Shell.
In the power shell window type the following command:
New-SPContentDatabase -Name ContentDbName -WebApplication WebApplicationName
 -
DatabaseServer DBServerName

Restoring Site Collection on Farm B.
In the power shell window type the following command:
Restore-SPSite –Identity FarmBSiteCollection -Path TempPathAndFileName.bak
-DatabaseServer DBServerName -DatabaseName ContentDbName

Restore Access/Permissions
You need to add a user via command line because the restored site will deny all of your login attempts (unless your server is on the same domain as the original source server).

Using the central administration console, you will note that the site collection still references the original administrators which are now invalid on this new domain.
NOTE: you can view using following steps >> Central Administration >> Application Management >> Site Collection Administrators (on Farm B SharePoint central administration domain).

·         Now you need to change ownership of the site collection using following command.
Set-SPSite –Identity FarmBSiteCollection -OwnerAlias “domain/User”
·         Now if you refresh central admin console you will see the new administrator as an owner, you can change the secondary administrator at this point as well using same utility.
Set-SPSite –Identity FarmBSiteCollection - SecondaryOwnerAlias “domain/User”
·         Now reset the IIS and you can login the main site collection as an administrator.
·         Then you can optionally run this to clean up the old lingering logins explicitly
Move-SPUser –Identity olddomain\UserName  - NewAlias newdomain\UserName

Wednesday, May 18, 2011

BitCoins???? Fascinating concept.

Came across this article not sure I understand all the concept but what an idea.
Is this really going to take off or just die a slow death like many cool concept?


Wednesday, March 16, 2011

Reader Quotas for WCF Services in SharePoint 2010

Creating a WCF service in SharePoint 2010 is relatively easy.  The first thing to do is follow the basic steps in Microsoft's post "Creating a Custom WCF Service in SharePoint Foundation".  This will give you a nice starting point for your Service as long as you don't need to need to change any of the Binding information.

Try sending a 70k file to your nice new WCF service.  Oops  error!

The remote server returned an unexpected response: (400) Bad Request.*
Or

The maximum string content length quota (8192) has been exceeded while reading XML data. This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader.

Ok no problem.  If you have done any WCF Service code in the past you probably know that you need to alter some of the binding information to increase the message size and or change the attributes for the readerQuotas no big deal right? 

Wrong!!! 

SharePoint 2010 has this great new hosting ability MultipleBaseAddressBasicHttpBindingServiceHostFactory and it does all the work required to set up the end points and bindings. You don't need to do create a web.config file or write any WCF service code.

So right about now you should be asking yourself "Self, how do I change the binding information then?".
Well after hunting around for the better part of a day I found a couple of post that talked about using the SPWebService.ContentService to programmatic change the binding information.
For example:


But no where do any of the post talk about where do I run this code.  Wouldn't make sense to run it on the client, although you do need to make some changes on the client side as well.  And running it as a Event Receiver when the feature for the WCF Service is installed did not work for me ether.

So what next???
Well I finally came across a comment on one of the MSDN pages by Dan Mayernik.

First, to update the configuration for a custom WCF service -add a WebApplication level feature with nothing more then a feature receiver. The receiver should override "FeatureInstalled" as follows:



Obviously, the values listed above are the extreme cases and should be set according to what your service will need. Also, the key for the indexer used for the WcfServiceSettings collection is the name of your service file. If you place the file in a subdirectory -do not include the subdirectory name as part of the key.

Thus, if your service is deployed to the ISAPI directory under:
/MyCompany/MyCustomService.svc

the key for the WcfServiceSettings collection would be:
mycustomservice.svc
 

CAUTION: The key, as stated above, is case sensitive. The key should be all lowercase.



Thanks goes to Dan he saved me from loosing any more of my hair (this week)