Thought I don't encourage anyone to use it over Sharepoint web services but there may be situations where you will have to use it. Sharepoint web services are the best way to deal with any sort of Sharepoint issues from outside of Sharepoint. And before using SP OM keep it in mind that The object model can be used when the application will run on the server where SharePoint is installed or in assemblies that are run within a site (such as a Web Part).
Here I am showing how to use SP OM in two types of application, Console and Web.
First console.
1. Create a console application and add reference to Microsoft.Sharepoint to your project. In case of SP2010 you will find it in "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.dll" and in case of SP2007 you will find it in "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.SharePoint.dll"
2. Add the following code to your project.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace test
{
class Program
{
static void Main(string[] args)
{
string siteStr = "<SP Site URL>";
SPSite tempSite = new SPSite(siteStr);
SPUserToken systoken = tempSite.SystemAccount.UserToken;
using (SPSite site = new SPSite(siteStr, systoken))
{
using (SPWeb web = site.OpenWeb())
{
//right now, logged in as Site System Account
Console.WriteLine("Currently logged in as: " + web.CurrentUser.ToString());
Console.Read();
}
}
}
}
}
2. Now go to the "Properties" window of the project and select "Application" tab. Change the "Target Framework" to ".NET Framework 3.5". Now go to "Build" tab and change the "Platform Target" to "x64".
Now run your application. You should see something like "Currently logged in as: SHAREPOINT\system".
For Web appliaction.
1. Create a ASP.NET web site and add the following code in your .aspx page's code behind. Add Microsoft.Sharepoint like before.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string siteStr = "http://dacsvm0021/my/personal/SSO/";
SPSite tempSite = null;
SPSecurity.RunWithElevatedPrivileges(delegate(){
tempSite = new SPSite(siteStr);
});
SPUserToken systoken = tempSite.SystemAccount.UserToken;
using (SPSite site = new SPSite(siteStr, systoken))
{
using (SPWeb web = site.OpenWeb())
{
Label1.Text = "Currently logged in as: "+web.CurrentUser.ToString();
}
}
}
}
As you can guess I have created a Label "Label1" in my page.
2. Create a web site in IIS. Now here is a small trick. While creating the site do not create a new application pool for your site, rather use the application pool of your Sharepoint site.
3. Now deploy your site to your IIS site.
Now if you browse the site you should see something like "Currently logged in as: SHAREPOINT\system".
After doing all these you still may have issues with Object Model. One popular but unauthenticated suggestion is to run all your application in "Administrator" mode. If still you have issues then beg help from Allah!
No comments:
Post a Comment