Friday, July 15, 2011

LinQ to SharePoint examples

Namespaces to refer in Visual studio for using LinQ

using System.Linq;
using Microsoft.SharePoint.Linq;
using System.Collections.Generic;

Generate Entity classes using SPMetal
Run the following command to generate it
spmetal /web:http:// /code:

Creating a Join between two list UserAccounts and Invoices.
List UserAccounts have Account, Name
List Invoices have Account, Amount, Paid

//First refer the Datacontext class
XDataContext a = new XDataContext(SPContext.Current.Web.Url);


//Refere the Lists and create entityLists out of them

EntityList Invoices = a.GetList("Invoices") ;
EntityList UserAccounts = a.GetList("UserAccounts");

//need to do a join from the two lists and bring them together.
//use the ToList() on the list in the from to avoid getting errors due to inefficient CAML queries. with this method it retrives data from the list instantaniously.

var query=
(from u in UserAccounts.ToList()
join i in Invoices on u.Account equals i.Account
where int.Parse(u.Account.ToString()) > 1001 && int.Parse(u.Title.ToString()) < 10010
select new { u.Name, u.Account, i.Amount, i.Paid }) ;

//make this as the datasource.GridView1.DataSource = query;
GridView1.DataBind();


Inserting in Lists using LinQ

XDataContext a = new XDataContext(SPContext.Current.Web.Url);

EntityList Invoices = a.GetList("Invoices");

int count = 0;
//get the count of the records
count =
(from x in Invoices
select x).Count();

List ic = new List();

if (count == 0)
{
    //insert 5K records
    for (int i = 0; i < 300; i++)
   {
       //create records and store them in a system.collection.generic.list collection
       InvoicesItem inv = new InvoicesItem() { Account = "100" + i.ToString(), Amount = 100 + i, Paid = check(i) };
        ic.Add(inv);
   }

//insert all of them in shot
Invoices.InsertAllOnSubmit(ic);
a.SubmitChanges();
}

//here is the check function that is being used
private Boolean check(int i)

{
if (i % 2 == 0)
return false;
else
return true;
}

Wednesday, June 8, 2011

Host header site collection VS Path based Site Collections in SharePoint 2010

SharePoint gives the ability to create site collections with it's own domain name while the web application has a different domain name, these site collections are called host name site collections and their creation and behaviour is different from regular path based site collections which by default are created under http://sp2010/SITES/XYZ/. Below are 10 differences that i could gather which lays out the difference between hostname site collections and path based site collections.

1. SharePoint 2010 Support
Host-Header Site Collection : Yes
Path Based Site Collection : Yes

2. DNS Support
Host-Header Site Collection : Unique DNS name for each site collection i.e. www.customer1.com, www.customer2.com or *.customer1.com

Path Based Site Collection : Will to be the same as the webapplication

3. Use of Managed Path
Host-Header Site Collection : Yes
• Host-Header Managed paths are different from Path based managed paths and cannot be used for each other.
• Managed paths created for host-named site collections are available to all host-named site collections within the farm regardless of which Web application the host-named site collection is in.

Path Based Site Collection : Yes
• Host-Header Managed paths cannot be used for Path based site collections and Vice versa

4. Creating Managed Path
Host-Header Site Collection : Powershell
New-SPManagedPath pathname –HostHeader

Path Based Site Collection : Powershell, CA
New-SPManagedPath “Teams” -WebApplication “http://somestuff”

5. Alternate Access mapping Support
Host-Header Site Collection : No, since they are always in the same Default zone.
Path Based Site Collection : Yes. And so supports multiple host header URL of its Web application using AAM

6. Co-Exist in the same Web application
Host-Header Site Collection : Yes
• As long as the host header bindings on the IIS Web site is not assigned to the Default zone of the Web application.
• Host header bindings in the IIS Web site can be applied to other zones of the Web application, this way Default zone will be used for host-named site collections allowing path-based site collections in other zones making use of alternate access mapping functionality.

Path Based Site Collection : Yes

7. Multiple in Single Web application
Host-Header Site Collection :Yes
Path Based Site Collection : Yes

8. Creating site collection
Host-Header Site Collection : PowerShell
New-SPSite http://www.contoso.com -OwnerAlias DOMAIN\username - HostHeaderWebApplication http://servername/
Above command creates site collection with the url www.contoso.com under the web application http://servername
Path Based Site Collection : PowerShell, Central Admin
New-SPSite http://sitename/sites/test -OwnerAlias "DOMAIN\JDoe"
Above command creates path site collection under the web application

9. Protocol Support ( HTTP\ SSL)
Host-Header Site Collection :
• Support one at a time either HTTP or SSL depending on the Default zone of the Web application under which it is created.
• Need to create another web applicatoin with the default zone being https to have site collection as HTTPS

Path Based Site Collection :
Supports both simultaneously since it’s web application can support multiple SSL certificate Each path based site collection shares the same certificate assigned to the web application.

10. SSL Certificate Type
Host-Header Site Collection :
• Only Wildcard certs ( applies to subdomain of the same domain) and Subject Alternate Name Certs(protects multiple host names with a single SSL cert) can be used .
• Cannot apply multiple SSL to unique domain, need to create different web application for each then.

Path Based Site Collection :
Cert are based on Web application and not site collection. All certs that can be applied on the web application applies to the site collection.

Tuesday, June 7, 2011

Adding Custom Styles to Ribbon’s Styles dropdown



Cool part about SP2010 is that as soon as it sees styles in the custom CSS file, it will add it to the Style dropdown, I made a quick documentation for a colleague so I thought would blog it as well.

You see the “Styles” in the Ribbon that is where I want to add my custom styles





This is a 2 step process:

1. All the custom styles in the custom css file should has the prefix .ms-rtestyle- and the value specified in the –ms-name: attribute will be displayed in the above dropdown



.ms-rteStyle-header {

-ms-name:"My Custom Header";

font-family: Calibri;

font-size: 19pt;

font-weight: normal;

color: #ef383f;

}



2. There are 2 ways to attach you Custom CSS file to the masterpage

Option 1 : Either specify the custom CSS file as an alternateCSS file by gong to CA\Site Settings\ Look and Feel\ Master Page ( you will have to activate the publishing feature for this option to be available)





OR

Option 2 : Just add a Link to the CSS file in the master page





Moving content using the granular “Perform a Site Collection Backup” option in SharePoint 2010

Good part about it is that using this option everything within a site collection will be backedup including workflows!
Backup Process

Backup of the site collection can be done using CA but the restore has to be done using power shell command, I am going to take you step by step through both the backup and restore.

Click the “Perform a site collection backup” option and select the site collection that you want to backup



Enter the Location and filename where you want to backup and hit on “Start BackUp’ which will create a backup of the site collection

Restore Process
Login to the destination farm as a user that has permissions to run powershell and type the following cmdlt restore-spsite, below is the command that I ran to restore the site collection to my webapplication, using the force parameter in case there is already an existing sitecollection there:

Restore-spsite -identity http://mysharepointserver/ -path C:\databasename.bak -force

Creating site Collection in its own Database


Creating site collection form CA is one of the easiest things but creating it with its own database is a little different, there are several ways to do it but I am going to do it using powershell:

//Get the webapplication in $wb variable
$wb = Get-SPWebApplication -Identity http://MySharePointServer:9001

//Get the desired name of the site collection’s database, we will create this DB later
$dbname = “Wss_content_MySite”

//Get url of the desired site collection
$SiteCollectionUrl = “http:// MySharePointServer:9001/”

//Create the new database
new-spcontentdatabase $dbname -databaseserver "SP2010Database" -webapplication $wb

//Create the site collection
new-spsite –url $SiteCollectionUrl -owneralias MyDomain\OwnerOfSiteCollection -Contentdatabase $dbname

After this is done and the first time you login to the site collection you will be prompted to select the template for the site and the users.

Friday, October 15, 2010

Implementing SSL on SharePoint 2010 and IIS 7 and forcing http request to redirect to https

Assumptions:

1. Certificate is already imported in IIS
2. I am using a wild card cert here but I believe it will be pretty much the same for other types of certs as well.
3. Port 443 is configured to point to this web application also.
4.WebApplications are using host headers


Open up IIS 7.0








Right Click on the website and select “Edit Bindings” which opens up the windows below





Click on add and Add https binding, select the SSL certificate in my case it is a wild card cert





Hit on OK



Now Lets change the port 80 to any other port, lets say 8026









Create a Port80 to Port 443 Redirector Dummy Website :


Select the Sites node under IIS, right click and select properties






Enter the below valuues, I just make it point to the Root IIS folder, this is just a dummy website and does not have any files in it. Hit on OK







So this setup means that all request for my test1..com over http will be coming to this webapplication, now lets configure redirection for these requests coming over http to https. Select the dummy webapplicatin and select ‘Http Redirect’ on the right hand side pane.







Sharepoint configuration :

Go to CA\ AAM\ hit on Edit Public URLs and enter the following which is
Default : https://test1..com
Intranet : http://test1..com


 Save it to look like this.



Now try accessing http and it should redirect to https!
But this will break search and Excel services, lets go fix that up.
Go to CA\Application management \ Manage Service applications\



 Click on the instance of “Search Service Appilcation” that is indexing your website.


Click on the “content Sources” on the left hand side



 Click on the content source that has your webapplication, in my case it is “Local SahrePoint sites”.
In here change your website to use https instead of http and then start a new Crawl by check marking “Start Full Crawl of this content source”



 Check the Crawl Log to make sure there are no errors.


Excel services : lets fix this up , I require an https trusted location to store files.
Go to CA\Application Management\ Manage service application and select the Excel Service Application instance to which your web application is associated with.






Click Trusted File Locations to open it up





Click on Add Trusted file Location and enter the following :
Address : https
Location type : Microsoft SharePoint Foundation
Trusted Children : check mark it.


Confirm that excel files are opening up and search is worknig as well

Thursday, September 16, 2010

Have SharePoint 2010 open office documents in browser on an live farm

This blog walks through the process of configuring a sharepoint server so that office documents open up in the browser by default for reading and editing. Also for those who are new to Office web apps, Installing Office Web app is the only way to achieve this.

Blog assumes that sharepoint is already installed and configured. Installing office web app on a sharepoint farm that is not configured saves the extra step of re-running the config wizard.

Folllwing are the steps :
1. Install Office Web App on the Server

2. When installation is completed it will open the sharepoint 2010 product configuration wizard.

3. If this wizard is not run then even though office web app is isntalled on the server sharepoint will not recognize it and If you go to Central Admin\ Security settings\ Manage services on Server you will still not see any of the office web app service
a. Word Viewing service
b. Excel service
c. Powerpoint service

4. Run the sharepoint 2010 product configuration wizard
a. Log in to the sharepoint server as the DataAccess account that was configured but this farm and start the Configuation wizard, running this while logged in as any other user can cause the wizard to fail with a database unaccessible error.

































b. When prompted with the alert that some of the services needs to be restarted, click on Yes















c. After the config run successfully it will make sure that sharepoint registers office web app services.

5. Start the Service: Go to CA\ security setting\ manage services on server and start the services that office web app installed
a. Word viewing service
b. Excel calculaltion services
c. PowerPoint services





























6. Create a Proxy for the web office app services that you need : I will demonstrate it for the “word viewing service”. Go to Central Administration\ Application management\ Manage service applications and click on New and select “Word Viewing Service”





























7. Create the new Word viewing service application with the following settings, feel free to improvise depending on your environment.
i. Name the service “Word Viewing Service Application”
ii. Select the application pool – SharePoint web services Default
iii. Add to default proxy list – checkmark the option.





























8. Make sure that this service is associted with you Web application, If it is not then you can do it by going to CA\ Applicatoin management\ Configure Service application association.






























9. Enable a Site collection to use Office Web app :
a. Before office web apps can be used office web app feature has to be enabled, you will have to install it on all the wfes.
b. Deactivate the feature “Open Documents in Client Applications by Default” in case it is Active.
c. Go to the web application for which you want to enable viewing word documents in the browser, go to site actions\ site collection features and activate Office Web Apps feature




























This should make sure you can open office documents in the browser. IR, Safari, Firefox are supported, hopefully this blog helps you, leave a comment.