Wednesday, June 3, 2009

Server and Security settings for app to manipulate MOSS

In order to create web application/site collection and manipulate SharePoint programmatically, few things you need to ensure, without these you cannot be successful.

You can develop the program as console app, web app or any kind of service. But make sure

1. The server where the app is running from, should be having SharePoint binary installed and being added in the same farm. If all the servers in the farm is 64 bit make sure the concerned server is also having same OS version installed. If you are adding the server in the farm, it would never find the required SP context and hence web app itself cant be found.

2. Identity should be a domain account

3. Identity with which the app is running it should be having

- The db_owner role for content databases and search databases associated with the Web application
- The identity being used to create a web application / site collection needs to have write access on the configuration database and the SharePoint_AdminContent database.
- Access to read from and write to the associated SSP database.

4. Identity may not be part of farm admin group in order to follow least privilege access.

5. If you are not creating site collection or web app, you can very well use App Pool ID of the web app as an identity. But to keep separate security audit trail, you better use different account.

Monday, April 13, 2009

Activate the feature and add web parts through code

I would provide step by step process to add a web part in the web part gallery for a spcific site and activate the feature to show it in the default.aspx page.

I assume you have created the web part separately and placed it in the GAC and added in the safecontrol.

I am not showing you the whole solution but tell you the important steps and functions so that you can use it in your program.


1. Prepare DWP file through your code and add the webpart file in the web part gallery

public static bool AddWebPartsToGallery(string SiteURL)
{
bool isSuccess = false; //assume failure

string dwpUserControlContainer = @"" +
"" +
"" +
" Demo Web Part.
" +
"DemWebPart,Version=1.0.0.0,Culture=neutral,PublicKeyToken=2d5a04d2e0ff8065" +
"DemoWebParts " +
"
";
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SiteURL))
{
using (SPWeb web = site.OpenWeb())
{
SPList webPartGallery = web.GetCatalog(SPListTemplateType.WebPartCatalog);
SPFolder folder = webPartGallery.RootFolder;
web.AllowUnsafeUpdates = true;
if (folder != null)
{
folder.Files.Add("DemoWebpart.dwp", dwpUserControlContainer, true);
folder.Update();
isSuccess = true;
}
web.AllowUnsafeUpdates = false;
}
}
});
}
catch (Exception ex)
{
isSuccess = false;
}
return isSuccess;
}

2. Activate your feature and add the web part in the default.aspx

private void ActivateFeature(string SiteU
{

try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SiteURL))
{
using (SPWeb web = site.OpenWeb())
{
System.Globalization.CultureInfo oCultureInfo = new System.Globalization.CultureInfo(1033);
SPFeatureDefinitionCollection collFeatureDefinitions = SPFarm.Local.FeatureDefinitions;
foreach (SPFeatureDefinition oFeatureDefinition in collFeatureDefinitions)
{
if (oFeatureDefinition.GetTitle(oCultureInfo) == “Your feature name”)
{
Guid guidFeatureDefinitionID = oFeatureDefinition.Id;

if (oFeatureDefinition.Scope == SPFeatureScope.Site)
{
SPFeatureCollection collFeatureCollection = site.Features;
SPFeature oFeature = collFeatureCollection.Add(guidFeatureDefinitionID);
break;
}

}
}
}
}
});
}
catch
{
}

}
private static bool AddToPage(string SiteURL, string dwp)
{
bool isSuccess = false; //assume failure

try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SiteURL))
{
using (SPWeb web = site.OpenWeb())
{
SPWebPartCollection coll = web.GetWebPartCollection("default.aspx", Storage.Shared);
coll.Add(dwp);
isSuccess = true;
}
}
});
}
catch (Exception ex)
{
LogException(ex);
isSuccess = false;
}
return isSuccess;
}

Friday, April 3, 2009

Creating Subsite programmatically: RunWithElevatedPrivileges context

When you create a sub site with the same context of SPSite, you need to be smart enough to choose your code.

using (SPSite siteCollection = new SPSite(parentSiteURL))
{
SPWeb parentWeb = siteCollection.OpenWeb();//OPen the parent web

//Start code to get all available web templates
:
:
//End code

parentWeb.AllowUnsafeUpdates = true;

SPWeb AssignWeb = parentWeb.Webs.Add(siteURLRequested, siteTitle, "", Convert.ToUInt32(LOCALE_ID_ENGLISH), siteTemplate, false, false);


This code would give Access denied exception. Reason is when you are opening the web, site opening context does not hold good. Even if you put creation of subsite in another RunWithElevatedPrivileges, it wont work as new web would be created with another context. In order to resolve it replace parentWeb.Webs.Add with the following code

SPWeb AssignWeb = siteCollection.AllWebs.Add(siteURLRequested, siteTitle, "", Convert.ToUInt32(LOCALE_ID_ENGLISH), siteTemplate, false, false);

Monday, March 30, 2009

Creating Site Collection in FBA

Using WSS object model to create site collection is not that new but I have come across a scenario of creating site collection in form based authenticated web apps. I hope the following would help you to achieve the same as you need to make certain changes in your client to make it work.

For example, you have a windows service which is running with certain service account which has previelege to create site collection and other objects.

Step 1: Open you web.config of the SharePoint web app. Copy the following section and place it the app.config of your windows service. This is required for windows service to refer the user identity, otherwise it will throw "User not found" exception.






Step 2: In your code use SPSecurity.RunWithElevatedPrivileges carefully. See the following code

SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebApplication spWebApp = new SPSite("url").WebApplication;
SPSiteCollection spscol = spWebApp.Sites;

SPSite newSiteCollection = spscol.Add("path",
"FBAUser",
"",
1033,
"sts#1",
"FBA_AspNetSqlMembershipProvider:FBAUser",
"FBA User",
"FBA User");
});

SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite newSiteCollection = new SPSite("url");

SPWeb newSCweb = newSiteCollection.OpenWeb();

newSCweb.AllowUnsafeUpdates = true;

//Code to create the custom List
#region Creating Custom List
SPListCollection FLListColl = newSCweb.Lists;
:
:
:
});

First block of RunWithElevatedPrivileges ends with site creation. Once you try to open the web with same security context, you might face access denied exception. Hence you close the context and create new elevated security context.

I hope with this you are set to go.