This is another code tip posting. This will have different code snippets that may be useful when developing SharePoint applications. The code accomplished various things and I will give you an idea how you may want to use them in your applications. The code is posted “AS IS” with no warranties and confers no rights.
Turn On and Off Remote Administration
Many times your receive “access denied” errors even when you are running with elevated privileges. This is very frustrating when you expect to be able to activate features from code that may want to create new timer jobs. According to Microsoft http://support.microsoft.com/kb/2564009 this is due to a new security feature implemented with SP2010. The feature explicitly blocks modification to any objects inheriting from SPPersistedObject in the Microsoft.SharePoint.Administration namespace. However, there are many other places where this type of checking occurs outside of this namespace. In particular Microsoft.Office.Server.Search. In addition it is not always about modifications but even reading values. For example just trying to access Microsoft.Office.Server.Search.Administration.CrawledProperty will throw an access denied error even when privileges have been elevated.
You can get around this problem by setting the SPWebService.RemoteAdministratorAccessDenied property to false. The Microsoft.SharePoint.ApplicationRuntime.SPRequestModule.EnsureInitialize method is the only code that uses this property. However, it uses it to set the SPSecurity..AdministrationAllowedInCurrentProcess property. This property is used a great deal throughout SharePoint to turn on access to resources SharePoint deems as configuration data that can only be access or modified from Central Administration .
The following code shows how you can set this property. It must be set from the ContentService.
public void EnableRemoteAdministration(bool enable)
{
SPWebService.ContentService.RemoteAdministratorAccessDenied = !enable;
SPWebService.ContentService.Update(true);
}
Just remember you cannot use this like you do SPSecurity.RunWithElevatedPrivileges. This is because when setting this property it checks to see if the current logged in user is a farm administrator or a member of the local administrators group. Once this property is set to false in most cases where you received and access denied error after elevating privileges, this error will no longer happen.
Setting the Default Group for a Site Collection
There are scenarios where you want a group to show up first in the list of groups when setting permissions. For example when granting permissions to a user in Site Permissions page of the site collection.
This group could be the most common group a user is added to. By making it the default group it will make assigning permissions much faster.
public static void SetDefaultGroupForWeb()
{
using (SPSite site = new SPSite("http://basesmc2008"))
{
using (SPWeb web = site.OpenWeb())
{
SPGroup group = web.SiteGroups["Home Visitors"];
web.AssociatedMemberGroup = group;
web.Update();
}
}
}
Editing URL (Link) Fields
I see a lot of questions on the forums of how to edit link fields. For example, how to set both the link (URL) and the description without having to parse “;#” string. It is straight forward to set these values using the SPFieldUrlValue.
public static void ModifyUrlFields()
{
using (SPSite site = new SPSite("http://basesmc2008"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["mynewlist"] as SPList;
SPListItem item = list.GetItemById(1) as SPListItem;
SPFieldUrlValue value = new SPFieldUrlValue(item["hyper"].ToString());
value.Url = "http://www.certdev.com";
value.Description = "very cool";
item["hyper"] = value;
item.Update();
}
}
}
View BarCodes in the SharePoint UI
You can enable a barcode to be generated automatically for all new documents uploaded using SharePoint’s out of the box information policy. However, there may be times you may want to display the actual image of the barcode in your views or view forms. By default the barcode image is not displayable.
The following code will enable the viewing of the barcode image.
public static void EnableBarcodeSelection()
{
using (SPSite site = new SPSite("http://basesmc2008"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["customlist"];
SPField field = list.Fields["Barcode Value"];
field.Sealed = false;
field.ReadOnlyField = false;
field.ShowInDisplayForm = true;
field.ShowInEditForm = true;
field.Update();
list.Update();
}
}
}
This concludes this collection of code tips. Granted you may never need these but they may be useful in the future.
1 comment:
Post a Comment