Monday, February 1, 2010

Creating Sites using Sharepoint Object Model


MOSS 2007 provides rich set of API’s that allows developers to programatically accomplish almost everything that can be done in sharepoint. Sharepoint Object Model comes with 10 different dll’s comprising 30 namespaces.

The aim of this post is to show the sharepoint object model to create a new sharepoint site programatically.

Before start, lets look at the sharepoint site structure in the following diagram.

SiteArchitecture



Each layer in the above architecture diagram is represented by an object in the sharepoint object model. The object mapping is represented in the following table.
Sharepoint Site Architecture Component Object in Sharepoint Object Model

Sharepoint Site Architecture Component Object in Sharepoint Object Model
Site Collection SPSite
Site SPWeb
List Collection SPListCollection
List SPList
List Field Collection SPFieldCollection
List Field SPField
List Item Collection SPListItemCollection
List Item SPListItem


Steps to create a new Sharepoint Site:

1. Open Visual Studio. Create a new Windows Forms project.
2. Add a textboxes, one to accept the name of the site collection and another to accept the name of the new site to be created under the site collection.
3. Add a button that creates the new site.
4. Add reference to Microsoft.Sharepoint dll. This dll is represented by the name Windows Sharepoint Services in the Add Reference dialog box.
5. Add the namespace of Microsoft.Sharepoint dll like this.
* using Microsoft.Sharepoint;
6. In the button click event, open the site collection.
* SPSite siteCollection = new SPSite(txtSiteCollectionUrl.Text);
7. Now open the top level site of the site collection.
* SPWeb site = siteCollection.OpenWeb();
* If you want to open a different site under the site collection other than the top level site, you can use the overloaded methods of the OpenWeb() method that accepts the name of the site as argument.
8. SPWeb object has a property called ‘Webs’ that contains a collection of all the sites present under that site.
9. Inorder to add a new site, use the following code.
* site.Webs.Add(txtSiteName.Text, txtSiteName.Text, Convert.ToUInt32(1033),site.WebTemplate, , false,false);
10. Close and dispose and the site and site collection
* site.dispose();
* siteCollection.close();

The complete code can be found in this file.using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

SPSite siteCollection;
SPWeb site;

private void btnCreateSite_Click(object sender, EventArgs e)
{

try
{
siteCollection = new SPSite(txtSiteCollection.Text);
site = siteCollection.OpenWeb();
site.Webs.Add(txtSiteName.Text, txtSiteName.Text,
txtSiteName.Text + ” Desc”, Convert.ToUInt32(1033), site.WebTemplate,
false, false);
}
catch (Exception)
{ }
finally
{
site.Dispose();
siteCollection.Close();
}
}

}
}

Sunday, January 31, 2010

Share Point Exception : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite mysite = new SPSite(txtURL.Text.Trim());

mysite.AllowUnsafeUpdates = true;
mysite.CatchAccessDeniedException = false;

foreach (SPWeb myweb in mysite.AllWebs)
{
//myweb.Title;
cboSites.Items.Add(myweb.Title);
}
});

Monday, December 21, 2009

Oracle: Find row count in multiple Tables

use the following query to selct number of rows in multiple tables

select table_name, num_rows from user_tables where lower(table_name) in ('Table1',
'Table2',
'Table3')

Oracle: Stored procedure for Analyzing Tables

1. Create the following procedure by changing the 'User Name'

CREATE OR REPLACE PROCEDURE analyze_tables IS
BEGIN
FOR syn_cur IN (SELECT table_name
FROM user_tables)
LOOP
DBMS_STATS.GATHER_TABLE_STATS(ownname => 'User Name',
tabname => syn_cur.TABLE_NAME,
estimate_percent => NULL,
cascade => TRUE);

END LOOP;

EXCEPTION
WHEN OTHERS THEN
null;
END analyze_tables;

2. Run the procedure as below

begin
-- Call the procedure
analyze_tables;
end;

That is it.

Wednesday, December 16, 2009

Resolving Can't Login To Sqlexpress Using Sa Account

When installing Visual Studio 2005 or Visual Studio 2008, the installer will automatically install for us one instance of SQL Server 2005 Express. But we can't actually connect the database without extra tools and we can't login except using Integrated Security. In this article I will explain how to download extra tools and how to login using sa account (not Windows Authentication Mode).

1. First download Management Studio Express for SQL Server Express 2005, http://www.microsoft.com/downloads/details...;displaylang=en this tool will enable you to connect with SQLExpress instance that is installed via Visual Studio. Install this tool and you can connect to database [MyComputerName]\SQLEXPRESS but for first you will need to use Windows Authentication mode. This way you can connect to the instance.

2. After success connecting the database, Open Security tree, Logins tree and you can see sa account. Double click 'sa' account and from General page, change the Password and Confirm Password to new password that you want. Then change to Status page, on Login section select Enabled. Then click OK.

3. Now right click the [MyComputerName]/SQLExpress and select Properties then open the Security page, on Server Authentication section select SQL Server and Windows Authentication Mode.


4. Open Services diaog box, (on Control Panel open Administrative Tools, then Services). Select the SQL Server (SQLEXPRESS) service, restart this service.

5. Now you can connect to SQLEXPRESS instance using Management Studio via sa account and new password you set.



The issues raise because by default Microsoft has (once again) made the free SQLEXPRESS by default instance only with Windows Authentication Mode (which means that it will accept connection only from integrated security, like from local computer or computer that has login for it). And by that reason, SQLEXPRESS is limited to developer, thus some developer does not know that this default behaviour can actually reconfigure. After configuration you may see that SQLEXPRESS can now act like a decent database, and even more if you read my other article here you can even configure the instance name SQLEXPRESS to whatever you like!

LINK:

Resolving Can't Login To Sqlexpress Using Sa Account

Tuesday, April 7, 2009

How to Remotely Restart a Windows Machine Through Command Line

The DOS command line built into every windows machine can be used to perform a lot of useful tasks. One very helpful task is shutting down your computer. By working your way through a few simple steps, you will find that you are able to easily shut down or restart your computer.

Steps


  1. Make sure you have an administrative rights on the remote machine and you can PING it.
  2. On your workstation - Click on Start > Run, Type shutdown -i
  3. select options to shutdown or restart
  4. Alternatively - Go to command prompt (start > run > cmd) on your workstation... and Type shutdown -r -m \\xxx.xxx.xxx.xxxReplace xxx.xxx.xxx.xxx with the IP address or computer name of the remote machine. -r option is for restart, don't use -r if want to just shut down the system.


Tips


  • Type shutdown -? for more options and control over shutdown command..


Warnings


  • Works only for windows servers and workstations.
  • Shutdown is not an inherent command in Windows Server. You need to install the Windows Server Admin Pack to get this functionality. Out of the box, this will not work.