Monday, 15 October 2012

Rewire Content Database

this is the final post in reference to restoring your content database. Here I'll cover how to rewire the db permissions so that your user can actually do something with the db.

lets set our focus back to Microsoft sql server management studio.

Check under security for your machine and make sure that the user account you’re concerned with is available. in my case it's ssrap7/Administrator and to remind you my content database is WSS_Content. as you can see below, ssrap7/administrator is not included in my content database so I'm going to go ahead and add it.

to add a new user simply right click on your content database and hit add user, just make sure to select db_owner, refer to image below.

now with with db_owner checked off click the OK button. Next run the following Powershell


function LoadSharePointPowerShellEnviroment
{
                write-host
                write-host "Setting up Powershell enviroment for Sharepoint" -foregroundcolor Blue
                Add-PSSnapin "Microsoft.Sharepoint.PowerShell" -ErrorAction SilentlyContinue
                Write-host "Sharepoint PowerShell Snapin loaded." -foregroundcolor Green
}
LoadSharePointPowerShellEnviroment
Mount-SPContentDatabase "WSS_Content" -DatabaseServer "SSRAP7" -WebApplication
http://SSRAP7/

make sure to specify your database name, database server, and web application URL.

once you've run the power shell, you must add your account as local site admin

Open central administration-> application management ->Select Change site Collection administrators

This will show you the site collection administrators set the primary site collection administrator to your local administrator


hit ok, and BAM, you're done.

Restore a Content Database

In my previous post I demonstrated how to back a content database up, which is all fine and dandy but ultimately useless if you can't restore it.

Go to the manage content databases screen in central administration.
Once there make sure you have the correct web application selected, and click on your content database.
Scroll down to the bottom of the options for your content database and check off "Remove content database" then click OK
When complete, you will be redirected to the “manage content database” screen in central administration with your content database no longer available.

Do the following before the restore:
Stop IIS; use iisreset /stop in the command prompt
Stop the following services
  • SharePoint 2010 Administration
  • SharePoint 2010 Timer
Open up SQL Server Management studio
Right click on the Database you just removed
Go to Tasks-> Restore-> Database...


On the restore Database dialogue, under Source for restore check From device

Then click the browse (...) all the way to the right of the from device: radio control. This will bring up the Specify Backup dialogue.

here select the back up file you wish to restore, refer to my previous post to find out how to make one.

Once you’re back to the restore database dialogue, make sure to actually select your back up file, by checking it off.


With your back up file selected click the options page in the left hand pane. This will bring up the options page.
Make sure to check off overwrite the existing Data base, then hit OK.

This will now restore the content database with production data; this process takes a while so now would be a great time to do some documentation or a coffee break.

Now remember, start up the two services you disabled previously
  • SharePoint 2010 Administration
  • SharePoint 2010 Timer
and start up IIS again iisreset

In the next post I discuss how to rewire your content database to work with your current user

Wednesday, 26 September 2012

Back up Content Database

How to back up your Content Database

In this Post I'm going to demonstrate how to back up your content database, in my next post I'll demonstrate how to restore your Content database from a backup. This is especially useful if you use a development box for new features and you would want to attach you production data to see what happens; I think some people call that testing.

So let's get started.
  • Open up central administration
    • In the Application management
    • Click on Manage Content Databases link (right)
  • This will bring up the content database for our site
    • Make sure that you have the correct web application selected.
    • Make note of the Content Database name




Next go ahead and open the Microsoft SQL Server Management studio

  • In the object Explorer expand the database folder
  • Find the Content Database you previously identified
  • Right click on it
  • Go to Tasks->Back Up...





This will bring you to the backup Dialogue


Make note of the backup destination or just change it to the root. Then click OK

Upon successful completion you’ll see

That's it you've now backed up your content Database.

Friday, 14 September 2012

Event receiver rewire lookup

In my previous three posts, we demonstrated how to:

  • Create site columns
  • Create lookup fields
  • Create content types
  • Create list definitions
  • Create list Instances
  • Rewire Lookups using a PS script

Not only all this, but we started to touch upon the Event receiver, and in this post that's what we're going to focus on. Lookup columns are great, they're just a pain in the arse with the whole rewire business; if only there was a way to automate the process. One guess at what we're about to do.

If you guessed share my secrete recipe for sweet potatoes, you could be more wrong. We are going to automate a lookup field rewire using the feature receiver.

First thing's first open up your project from my previous post and add an event receiver to your Lookup.Date feature. To do this, right click on the feature and select "Add Event Receiver"

with the Event Receiver added your solution explorer should look like
Now When you open your Lookup.EventReceiver.cs file you should see:
At the bottom of your Event Receiver add the following function
/// <summary>
/// Function used to rewire lookup fields
/// </summary>
/// <param name="ConsumerWeb">The web where the list that will be using the source resides</param>
/// <param name="ProvidorWeb">The web where the source list resides</param>
/// <param name="lookupFieldName">The display name of the lookup filed in the consumer</param>
/// <param name="ConsumerListName">The display name of the list that is using the lookup field</param>
/// <param name="ProvidorListName">The display name of the list that is supplying the data to be looked up</param>
public static void UpdateLookupField(SPWeb ConsumerWeb, SPWeb ProvidorWeb, string lookupFieldName, string ConsumerListName, string ProvidorListName)
    {
        SPList consumerList = ConsumerWeb.Lists[ConsumerListName];
        SPList providorList = ProvidorWeb.Lists[ProvidorListName];
        SPFieldLookup field = (SPFieldLookup)consumerList.Fields[lookupFieldName];
 
        field.SchemaXml = field.SchemaXml.Replace(field.LookupWebId.ToString(), ProvidorWeb.ID.ToString());
        field.SchemaXml = field.SchemaXml.Replace(field.LookupList.ToString(), providorList.ID.ToString());
 
        field.Update(true);
    }

with your rewire function added, the next step is to re comment the FeatureActivated function. once that's re-commented add the following code, but make sure to sub in your sites url and port number.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb web = properties.Feature.Parent as SPWeb;
 
    using (SPSite site = new SPSite(web.Site.ID))
        using (SPWeb providorWeb = site.RootWeb)
        {
            SPWeb ConsumerWeb = providorWeb.Webs["hr"].Webs["JobPosting"];
            UpdateLookupField(ConsumerWeb, providorWeb, "Department", "Job Posting", "Job Posting Departments");
        }
}

and that's it when you deploy your project your look up fields will automatically rewire.

Wednesday, 22 August 2012

Lookup Site Column

So in my past two posts, we created the site columns, content types, list definitions and list instances to create two lists, one for departments and one for job postings. In this post we are going to bring them together using a lookup column, this is going to let our job posting list feed from the departments list so that as we update our departments they are automatically available in our job posting list.

If you haven't gone through my previous posts here are the links, they're rather trivial if you're already familiar with SharePoint, but if you're a noob like me, then here you go.


Let's get started with our Lookup Site column, go back to your site columns elements file and add the following:

  <Field ID ="{C0CC842C-8059-482A-AE40-0B5601FB4B60}"
         Name ="SPJ_DepartmentLookup"
         DisplayName="Department Lookup"
         Type="Lookup"
         List="Lists/Departments"
         Title="Department"
         ShowField="Title"
         OverwriteInChildScopes="TRUE"
         Overwrite="TRUE" />

With our Site Column created next we have to add it to our Job Posting Content type, so go ahead pop open the JobPosting Content type and add it to the fields:
<FieldRef ID ="{C0CC842C-8059-482A-AE40-0B5601FB4B60}" Name ="SPJ_DepartmentLookup" />
Now Deploy your Lookup.Schema to add your new lookup site column to the site columns collection and to the job posting content type. With that complete you've come to a fork in the road. you may notice that just by adding the site column to the content type it doesn't affect your list instance at all. This is because your list instance in based on your list definition which used your content type at the time it was instantiated, that is to say that the connection doesn't exist when you build a list definition it uses the content type at the time of development and subsequent changes to the content type are not reflected in the list definition.

Your options are as follows
  • delete the list definition and rebuild it using your new content type
  • try to modify the list definition schema
I recommend the former over the latter of the options, once you've rebuilt your list definition go into the elements file and make sure that Type property is unique to all other list definitions.
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <!-- Do not change the value of the Name attribute below. 
         If it does not match the folder name of the List Definition project item, 
         an error will occur when the project is run. -->
    <ListTemplate
        Name="JobPosting_LD"
        Type="10001"
        BaseType="0"
        OnQuickLaunch="TRUE"
        SecurityBits="11"
        Sequence="410"
        DisplayName="JobPosting_LD"
        Description="My List Definition"
        Image="/_layouts/images/itgen.png"/>
</Elements>
With that complete, open up the elements file for your list instance and ensure that the value of the TemplateType property matches up with the previous type value:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <ListInstance Title="Job Posting"
                OnQuickLaunch="TRUE"
                TemplateType="10001"
                FeatureId="a2c12d31-b651-4eee-9214-9f4d970d0b4a"
                Url="Lists/JobPosting"
                Description="List of all Corporate Job Postings">
  </ListInstance>
</Elements>
Now you can go ahead and deploy your Lookup.Data project, awesome great we're done now...
Unfortunately not so fast, SharePoint does this awesome thing where it gives a list instance a brand new GUID every time it's deployed, meaning that you have to rewire your lookup columns after every single deployment.

Luckily I have a script for you that will do the trick, execute the following script in PowerShell to fix your lookup column, just remember to specify your own URL and port number.

$url = "http://urlpath:port"
 
function LoadSharePointPowerShellEnviroment
{
write-host
write-host "Setting up Powershell enviroment for Sharepoint" -foregroundcolor Blue
Add-PSSnapin "Microsoft.Sharepoint.PowerShell" -ErrorAction SilentlyContinue
Write-host "Sharepoint PowerShell Snapin loaded." -foregroundcolor Green
}

try
{
    LoadSharePointPowerShellEnviroment
 
    $RootSite = new-object Microsoft.SharePoint.SPSite($url)
    $RootWeb = $RootSite.rootweb
 
$JobPostingList = $RootWeb.lists["Job Posting"]
$field = $JobPostingList.fields["Department Lookup"]
$DepartmentList = $RootWeb.lists["Department"]
 
    Write-Host -ForegroundColor red  $field.lookupWebId.toString() to $RootWeb.id.toString()
    Write-Host -ForegroundColor red  $field.lookupList.toString() to $DepartmentList.id.toString()
 
    $field.schemaXml = $field.schemaXml.replace($field.lookupWebId.toString(), $RootWeb.id.toString())
$field.schemaXml = $field.schemaXml.replace($field.lookupList.toString(), $DepartmentList.id.toString())
                 
    $field.pushChangesToLists = $true
    $field.update($true)

    Write-Host -ForegroundColor Green "Lookup lists rewired"
}
catch [Exception]
{
Write-Host -ForegroundColor Red "Error in  Rewire_Lookup_Fields: $_"
}
finally
{  
    $RootWeb.Dispose()
$RootSite.Dispose()
}
After running your script, your lookup column should be wired up.

Saturday, 18 August 2012

List Definition & Instance

In my previous post I held your hand through the tough process of declaring some site columns and content types; by tough I mean difficult if you're a nine year old with a learning disability, but hey I'm not one to judge. You can think of this post as part deux, where I will show you how to create a list definition from your content type and then instantiate a list instance from the definition. Sound like a blast? if you said yes, I sympathise with you for my life is just as empty and meaningless as yours. If you said no, well then congratulation and I'd like some fries with that...

Let's get started, create a SharePoint 2010 Project in the same solution called LookUp.Data, this is going to be where our list definitions and list instances are going to reside.

With your Lookup.Data project Created add two folders:

  • List Definitions
  • List Instances

Lets create some list definitions:

  1. Add a new item to the List Definition folder
    • In the left pane select SharePoint 2010, 
    • In the middle pane select List Definition from Content Type
    • Give your list definition a descriptive name, and somehow specify that it's a list definition. I added an _LD but you can do whatever you like.
    • Click the Add button 
  1. Next you'll see the SharePoint Customization Wizard:
    • Make sure to shorten the solution name
    • select the content type you want to build the definition from
    • uncheck the "Add a list instance for this list definition"
now repeat the previous steps to create the Job Posting List Definition, once complete you should end up with something along the lines of:
  1. Now with Both of our List Definitions complete lets go and create some List Instances to match.
    • Right click on the List Instance folder and add a new item.
    • in the left hand pane again select SharePoint 2010
    • In the middle pane pick List Instance
    • Give it a descriptive Name like "Department"
    • Click Add
  1. Next you'll see the SharePoint Customization Wizard:
    • Give your list instance a display name
    • select the List Definition you want to Instantiate
    • Fill out a description
    • specify the URL address for the list
    • and finally decide whether you want it in the quick launch bar, that's really up to you, generally if a list is updated regularly by a user you'd set it to true.
Now with the Department List Instance ready repeat the steps above to create the Job Posting List instance.

Just as before lets clean up our feature:

  • Rename Feature 1 to "Lookup.Data"
  • Double Click the Lookup.Data feature to view the feature manifest.
    • Change the Title to something descriptive
    • Give it a description
    • set the scope
    • and most importantly remember to add all the List instances to the manifest.
Once Complete your Feature Manifest should resemble the following.

Now deploy the Lookup.Data project to deploy our list definitions and instances. Now you need to know that when you deploy a project that contains a list instance two shitty things happen:
  • all list data is cleared
  • the list is assigned a new GUID (that ruins lookup columns)
I mention these because in the next post we're going to add a lookup column and then rewire it.

Friday, 17 August 2012

Site Columns & Content Types


You want to Create Two Lists one that feeds off the other, but you're not sure where to start, well you start with site columns & Content types then you move onto List definitions and Instances.

Let’s get started, create a new SharePoint 2010 project and name it Lookup.Schema inside a solution called Lookup.Example, this is where we are going to put the site columns, and the content types for our two lists. Create two folders:

  • Content Types
  • Site Columns

Once that’s complete inside the SiteColumns folder create an Empty Element called SiteColumns and create the following site columns
  •  DepartmentID – Text
  • Department – Text
  • JobPostingName – Text
  • StartDate –DateTime
  • EndDate –DateTime
  • Department – Lookup

The XML for the Site Columns:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Field ID ="{B23D01D7-5062-4CBC-8336-2FF4E1731ABE}"
       Name="SPJ_DepartmentName"
       DisplayName ="Department"
       Type="Text"
       Required="TRUE"
       Group="SPJ Columns" />
  <Field ID ="{FE0E46B4-ECC0-4CAB-84AC-7D835B486834}"
       Name="SPJ_DepartmentID"
       DisplayName ="Department ID"
       Type="Text"
       Required="TRUE"
       Group="SPJ Columns" />
  <Field ID ="{07D51A7C-1125-412D-8999-D4A6F8F6F5BA}"
       Name="SPJ_JobPostingName"
       DisplayName ="Job Posting"
       Type="Text"
       Required="TRUE"
       Group="SPJ Columns" />
  <Field ID ="{E7A84BA3-1F9A-43AE-B5A6-07A3F1A726D8}"
       Name="SPJ_StartDate"
       DisplayName ="Start Date"
       Type="DateTime"
       Required="TRUE"
       Format="DateTime"
       Group="SPJ Columns" />
  <Field ID ="{9861E969-637A-49F3-8354-A1D0BB65B14D}"
       Name="SPJ_EndDate"
       DisplayName ="End Date"
       Type="DateTime"
       Required="TRUE"
       Format="DateTime"
       Group="SPJ Columns" />
</Elements>

Now what you might notice is that I’ve listed 6 columns that you need to make but there’s only definitions for five of them. As you will see later on, a lookup site column needs a list instance to feed from, so we’ll set that up once we have our provider list instance ready.

Now that we have our site columns ready to go we need to create Content type for our list definitions. I want you to create to Content types that inherit from Item in the Content Type folder:
  • Department
  • JobPosting
When you see the following screen make sure to select item from the drop down before you click Finish.


With your two content types created your solution explorer should resemble the following:

The Site column xml should be contained within the site columns empty element which is inside the Site Columns folder as for the department and JobPosting content types, they should be inside the Content Types folder. This may seem like overkill for a small demo project like this and it is, but it’s a great habit to get into. SharePoint Projects tend to grow at an exponential rate and due to the brittle nature of this beast it is very much worth wile to be organized from day one, rather than trying to come back later and organize things.

So let’s open up the Department Content type and add the site column detentions we are going to use:
  • Department ID
  • Department Name
The XML is as follows:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <!-- Parent ContentType: Item (0x01) -->
  <ContentType ID="0x010010495f58ea83494d8b3ece02c8ab1cf0"
               Name="Department"
               Group="SPJ Content Types"
               Description="Content type to describe a Department"
               Inherits="TRUE"
               Version="0">
    <FieldRefs>
      <FieldRef ID ="{FE0E46B4-ECC0-4CAB-84AC-7D835B486834}" Name="SPJ_DepartmentID"/>
      <FieldRef ID ="{B23D01D7-5062-4CBC-8336-2FF4E1731ABE}" Name="SPJ_DepartmentName"/>
    </FieldRefs>
  </ContentType>
</Elements>

With the Department Content Type complete let's make the Job Posting One:

  • Job Posting Name
  • Start Date
  • End Date

The XML is as Follows

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <!-- Parent ContentType: Item (0x01) -->
  <ContentType ID="0x010068497622be804676bf83a14bbdf3dc67"
               Name="JobPosting"
               Group="SPJ Content Types"
               Description="Content Type to describe a job posting"
               Inherits="TRUE"
               Version="0">
    <FieldRefs>
      <FieldRef ID ="{07D51A7C-1125-412D-8999-D4A6F8F6F5BA}" Name="SPJ_JobPostingName" />
      <FieldRef ID ="{E7A84BA3-1F9A-43AE-B5A6-07A3F1A726D8}" Name="SPJ_StartDate" />
      <FieldRef ID ="{9861E969-637A-49F3-8354-A1D0BB65B14D}" Name="SPJ_EndDate" />
    </FieldRefs>
  </ContentType>
</Elements>
With the Content types Complete lets add the finishing touches by expanding the Features folder in the solution explorer, rename Feature1 to Lookup.Schema.
With "Feature 1" renamed to "Lookup.Schema" double click on "Lookup.Schema.feature" to display the actual feature manifest. a feature manifest is a list of things that this feature is going to deploy to the site and activate. With your feature manifest open rename the title to Lookup.Schema WebScope and give it a description, also make sure that is in fact scoped for the web.

With all that complete deploy your solution to add the site columns and content types to your site.