SQL script to search all tables for a particular column name
By peter.stilgoe
Run the below script against a database to return all tables where a particular column name exists
use my_db SELECT t.name AS table_name, SCHEMA_NAME(schema_id) AS schema_name, c.name AS column_name FROM sys.tables AS t INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID WHERE c.name LIKE '<columnname>' ORDER BY schema_name, table_name;
How to create an Excel Workbook from an SQL database using Open XML SDK 2.0
By peter.stilgoe
Below is the C# code to create a new Excel workbook based on a template file & populated by an SQL database.
You need to add references to:
DocumentFormat.OpenXml
System.Data.Linq
Windows.Base
using System;
using System.IO;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml;
namespace ElitetoExcel
{
class Program
{
static string[] headerColumns =
new string[] { "A", "B", "C", "D", "E"};
static void Main(string[] args)
{
//Make a copy of the template file
File.Copy(@"c:\templatedoc.xlsx", @"c:\newdoc.xlsx", true);
//Open up the copied workbook
using (SpreadsheetDocument myWorkbook =
SpreadsheetDocument.Open(@"c:\newdoc.xlsx", true))
{
//Access the main workbook part which contains all the references
WorkbookPart workbookPart = myWorkbook.WorkbookPart;
//Grab the first worksheet
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
//Sheet data will contain all the data
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
//Connect to Database LINQ
DataClasses1DataContext
DataClasses1DataContext = new
DataClasses1DataContext();
int index = 2;
//select * from SPOpenMatters table
var MatterQuery = from t in DataClasses1DataContext.SPOpenMatters
select t;
//For each record in database add row in spreadsheet
Console.WriteLine("Creating Excel report...........");
foreach (var item in MatterQuery)
{
string MatterNum = item.matterno;
string Description = item.description;
string OpenedDate = item.openeddate.ToString();
string MatterStatus = item.matterstatus;
string BillingTKPR = item.billingTKPR;
//Add a new row
Row contentRow =
CreateContentRow(index, MatterNum, Description, OpenedDate, MatterStatus, BillingTKPR);
index++;
//Append new row to sheet data
sheetData.AppendChild(contentRow);
}
worksheetPart.Worksheet.Save();
}
}
private static Row CreateContentRow(int index, string MatterNum, string Description, string OpenedDate, string MatterStatus, string BillingTKPR)
{
//Create new row
Row r = new Row();
r.RowIndex = (UInt32)index;
//First cell is a text cell, so create it and append it
Cell firstCell =
CreateTextCell(headerColumns[0], MatterNum, index);
r.AppendChild(firstCell);//
//create cells that contain data
for (int i = 1; i < headerColumns.Length; i++)
{
Cell c = new Cell();
c.CellReference = headerColumns[i] + index;
CellValue v = new CellValue();
if (i == 1)
v.Text = Description.ToString();
if (i == 2)
v.Text = Convert.ToDateTime(OpenedDate).ToString("D");
if (i == 3)
v.Text = MatterStatus.ToString();
if (i == 4)
v.Text = BillingTKPR.ToString();
c.AppendChild(v);
r.AppendChild(c);
}
return r;
}
private static Cell CreateTextCell(string header,
string MatterNum, int index)
{
//Create a new inline string cell
Cell c = new Cell();
c.DataType = CellValues.InlineString;
c.CellReference = header + index;
//Add text to text cell
InlineString inlineString = new InlineString();
Text t = new Text();
t.Text = MatterNum;
inlineString.AppendChild(t);
c.AppendChild(inlineString);
return c;
}
}
}
Sharepoint 2010: Database doesnt exist but sill appears in Central Admin –> Manage Databases Upgrade Status
By peter.stilgoe
If you have deleted your database in SQL but they sill appear in Central Admin –> Manage Databases Upgrade Status, you can use the following Powershell script to remove them:
$db = Get-SPDatabase | where{$_.Name -eq "YourDatabaseName"}
$db.Delete()
Populate a Sharepoint list from an SQL Database
By peter.stilgoe
If you are wanting to populate a Sharepoint list from an SQL database or populate a SQL database from a Sharepoint list you can do this failry easily using SSIS & the SharePoint List Source and Destination Adapter project on Codeplex.
Introduction
This sample illustrates the creation of a a source and destination adapter in C#, using the public APIs for Microsoft SQL Server Integration Services. The component connects to SharePoint through a proxy dll, which wraps the complexities of working with the SharePoint Lists Webservice.
The Specific Features of the included SSIS Components are:
SharePoint Source Component
- Auto-detects all of the public fields from a SharePoint List or view of the list.
- Supports removal of fields from the output flow, which optimizes the query against SharePoint for increased performance.
- Supports restricting SharePoint data by a custom CAML query, which can be provided through a variable expression for fine tuned filtering over large lists.
- Provides a tuning feature for the # of rows to return at a time. Often with SharePoint, large lists need to be brought over in smaller batches than wide (many field) lists, or else the Webservice will die. The batchsize lets you specify a solid size that works for your list.
SharePoint Destination Component
- Auto-detects all of the editable fields from a SharePoint List
- Only fields bound to columns from the input flow will be ‘updated’, for performance.
- Supports deleting rows by ID
Included Bonus – SharePoint Utility
To simplify the adapter code and keep the SharePoint routines generic, a proxy VB.Net component is included which access the SharePoint Webservice API. It includes a very flexible api for getting SharePoint List information, which is used by the Components above. In addition, it can publish and remove files from a document library.
Component Features
Unique features that may inspire you to create great custom SSIS components:
• Extensive Validation: Validation for this component actively goes against the SharePoint Site to verify the properties are valid.
• Values from Expressions: This component supports external variables, which can be expressions, and can be attached to the source component to customize the query. Similar to the CommandText for the other Sql Components
• Linq: The Component has been written using Linq with .net 3.5 and shows how elements such as the metadata and columns can be combined to create a readable usage in a Linq format.
• Custom properties: The component keeps its configuration in custom properties on itself, inputs, and input columns.
• SharePointUtility Component: Simplifies access to the SharePoint Lists / Views WS using Linq friendly objects
Sharepoint List Source & Destination project on Codeplex
Error (Exception from HRESULT: 0×80004004 (E_ABORT))
By peter.stilgoe
This error usually occurs in Sharepoint when the transaction log is full or no drive space available to it on the SQL server in your farm.
You will usually get the error when trying to create new content etc.
More From pstilgoe
K2 deployment error: The partner transaction manager has disabled its support for remote/network transactions
By peter.stilgoe
When deploying a new K2 workflow / process you get an error message containing:
The partner transaction manager has disabled its support for remote/network transactions
First check on the application server(s):
1. Go to “Administrative Tools > Services”
2. Turn on the “Distribute Transaction Coordinator” Service if it is not running
If you still get the error:
First verify the “Distribute Transaction Coordinator” Service is
running on both database server and application servers
1. Go to “Administrative Tools > Services”
2. Turn on the “Distribute Transaction Coordinator” Service if it is not running
If it is running and its not running on the same server as the SQL Server:
1. Go to “Administrative Tools > Component Services”
2. On the left navigation tree, go to “Component Services > Computers
> My Computer” (you may need to double click and wait as some nodes
need time to expand)
3. Right click on “My Computer”, select “Properties”
4. Select “MSDTC” tab
5. Click “Security Configuration”
6. Make sure you check “Network DTC Access”, “Allow Remote Client”,
“Allow Inbound/Outbound”, “Enable TIP” (Some option may not be
necessary, have a try to get your configuration)
7. The service will restart
8. YOU MAY NEED TO REBOOT YOUR SERVER IF IT STILL DOESN’T WORK
On the application server(s) use the same above procedure to open the
“Security Configuration” setting, make sure you check “Network DTC
Access”, “Allow Inbound/Outbound” option, restart service and computer
if necessary.
On you SQL server service manager, click “Service” dropdown, select
“Distribute Transaction Coordinator”, it should be also running on
your server computer
More From pstilgoe
K2 – “Insufficient SQL database permissions for user in database ‘SharePoint_Config’ on SQL Server instance ‘WSS1′.
By peter.stilgoe
If your seeing the below error in your event viewer:
Error:
“Insufficient SQL database permissions for user in database ‘SharePoint_Config’ on SQL Server instance ‘WSS1′. Additional error information from SQL Server is included below.
EXECUTE permission denied on object ‘proc_putObject’, database ‘SharePoint_Config’, schema ‘dbo’.”
Solution:
The K2 Server account must be part of the Site Collection Admin Group in MOSS.
Save to sql server failed no description found
By peter.stilgoe
When trying to create an SISS package in sql server managament studio using the export wizard you get the following error message at the last stage:
“Save to sql server failed no description found”
To fix this you need to reregister the following DDL’s
Regsvr32 msxml3.dll
Regsvr32 msxml6.dll
Now when you try & save you SISS package it should go through with no problems.
More From pstilgoe
Import data from CSV and SQL to a SharePoint list
By peter.stilgoe
This SharePoint feature allows you to quickly import data from a CSV file or a SQL stored procedure to any custom SharePoint list.
Once you have added the solution to your farm and activated it on your site collection an additional menu option will be available on the Action menu of each custom list that will take you to the import CSV or SQL page.
Simply select which option you want – either import from a CSV file or import from a SQL stored procedure. Next, browse to the CSV file or enter the SQL connection and name of the stored procedure and hit the Import button.
There is a check box option to delete all items from the list before doing the import which is not selected by default.
More From pstilgoe
Sharepoint – Creating Content Databases
By peter.stilgoe
When you create a content database in Sharepoint through the central admin it is created with the following characteristics:
Recovery Model: FULL
Auto growth: 1mb
Log file growth: 10%
The one we are concerned with here is the Auto growth being set to 1mb. This means is we upload a 50mb file, the Db has to grow 50 x 1mb which causes performance issues. Also if a database auto grow occurs whilst being used by end users all web sites inside that site collection will stop while the database allocates more space. Also you will be pretty much guaranteed to get database fragmentation.
Its better to create your content databases manually and set the size you expect it to grow to from the outset. Then set a quota in Sharepoint so that the DB cant grow any more once it hits the size limit.
Note: if you do create via SQL ensure the DB settings are Latin1_General_CI_AS_KS_WS



January 30th, 2012
