Wednesday, April 30, 2014

SQL Server 2008 - Query XML data

Just recognized, that it is easy to query xml data stored in a SQL Server 2008 table (field):

declare @data xml

select @data = <datafield> from <table> (nolock) where <condition>

select @dataselect @data.query('<node>') as result

Here an example with sample xml:

declare @data xml
select @data = '<root><datas><data><add name="a" value="x1" /><add name="b" value="y2" /> <add name="c" value="z3" /></data><data><add name="a" value="e4" /> <add name="b" value="f5" /><add name="c" value="g6" /> </data></datas></root>' 
select @data
select @data.query('(/root/datas/data/add[@name="c"])[1]') as result 

The result is:

<add name="c" value="z3" /> 

Now I need to find out, how to get the value and not the complete node.

SharePoint 2010 - Using REST in client application

Hi guys,

today I want to show you, how you to get data from a SharePoint 2010 Server by using a REST client. 

Imagine we have a document library "How To" in a site collection "IT" on a SharePoint 2010 Server like this one:


We write a small console application to request all documents from this document library. The user can enter a search string and the results will be displayed after the input.

Ok, let's create a new solution in Visual Studio 2010:

I called the solution "REST-Solution".
Press "Ok" to create it.

After Visual Studio has created the solution, we have to add a service reference.

We have to enter the address to the service. In SharePoint 2010 we always find this service for each site in the "_vti_bin" folder. It's name is listdata.svc.
You can enter a different namespace in the text line. I called it ServiceReference1.


After adding the service reference to the project, we need to add 2 rows in the "using"-section:


Code Section: 
using REST_Solution.ServiceReference1;
using System.Net;
Note: In the first line, you have to use your solution name with the name of your service reference.

Then we can add our code.

Note:
The class name of the data context ("ctx" object) depends of the name of your Sharepoint site. In my case it is "IT", so the class name is "ITDataContext".
I think, the code is self-explanatory.

Code Section:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using REST_Solution.ServiceReference1;
using System.Net;
namespace REST_Solution
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create Uri
            Uri myUri = new Uri("http://testportal.grolman.de/sites/IT/_vti_bin/listdata.svc");
            // Create DataContext with Uri
            ITDataContext ctx = new ITDataContext(myUri);
            // Set Credentials
            ctx.Credentials = CredentialCache.DefaultCredentials;
            Console.WriteLine("Please enter search string:");
            // Get user input:
            string searchString = Console.ReadLine();
            // Create Query
            var linqQuery =
                from document in ctx.HowTo
                where document.Name.Contains(searchString)
                select document;
            // Results
            Console.WriteLine("Results:");
            foreach (HowToItem howtoItem in linqQuery)
            {
                Console.WriteLine("Document: " + howtoItem.Name + " / Version: " + howtoItem.Version);
            }
            // Stop here, wait for user input
            Console.ReadLine();
        }
    }
}
That's all. Start the program and you see it works fine:

All without using the client object model of SharePoint.

Wednesday, April 23, 2014

SharePoint 2010 - Document Templates

Here is a list for all available document templates of SharePoint 2010:

Template IDDescription
100No Template
101 Word 2003 document
103 Excel 2003 document
104 PowerPoint 2003 document
121 Word document
122 Excel document
123 PowerPoint document
111 OneNote Notebook
102 SharePoint Designer HTML document
105 ASPX Web Page
106 ASPX Web Part Page
1000 InfoPath document

SharePoint 2010 - Deleting a content type - Error message "The content type is in use"

I got mad with an issue. I have programmatically created content types and added them to new document libraries. I figured out some errors and I wanted to delete all and start from the scratch. So I deleted all document libraries and afterwards I tried to delete the content types - with the result: Error - "The content type is in use". :-(

Server Error in '/' Application.
The content type is in use.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.  
Exception Details: Microsoft.SharePoint.SPException: The content type is in use. 
Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 
Stack Trace:
[SPException: The content type is in use.]   Microsoft.SharePoint.SPContentTypeCollection.DeleteFromWeb(SPContentTypeId id,    String strName) +648   Microsoft.SharePoint.SPContentTypeCollection.Delete(SPContentTypeId id) +26928500   Microsoft.SharePoint.SPContentType.Delete() +85   Microsoft.SharePoint.ApplicationPages.ManageContentTypePage.DeleteContentType() +145   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,    String eventArgument) +29   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,    Boolean includeStagesAfterAsyncPoint) +2981
Version Information: Microsoft .NET Framework Version:2.0.50727.5477; ASP.NET Version:2.0.50727.5479 

I got mad with this issue, because I deleted everything, even I cleaned the recycle bin, which is visible on the pages. Nothing helped. Still got that error, but the solution is so simple:

Navigate to "Site Settings". A subpoint of "Site Collection Administration" is "Recycle bin".
You have 2 views:


Mark all items in both views and delete all.
Then it worked for me. Hope it solves your issue, too.