Lovely 12 Hive :)

A couple of hours ago, while surfing the MSDN Forums and I experienced a very interesting Question, well, let’s me share with you here the Question and my answer.

Q : We are creating a custom field type in a multilingual publishing site, we need to use the DisplayPattern to render the field value plus other Data that is language specific? Any ideas ?

A : Again , “The 12 hive is our teacher”.You know the little “New!” Icon that is displayed beside an Item when it’s newly created ?I’ve once created an Arabic/English site and I found out that the image is rendered in Arabic in the Arabic context and in English in the English Context which is great. Then you can definetly do that too either by Code or by CAML.
Let’s see if we can achieve that by means of CAML.
Open the 12 hive, Templates, Featues and open the folder of the fields feature, then open fieldswss.xml.
check the field titled “LinkTitleNoMenu” and let’s have a look at its display pattern.I’ve copied a part of it here, what I care about is the undlerlined part of the CAML below ..

<DisplayPattern>
<IfEqual>
<Expr1><LookupColumn Name=”FSObjType”/></Expr1>
<Expr2>1</Expr2>
<Then><Field Name=”LinkFilenameNoMenu”/></Then>
<Else>
<HTML><![CDATA[<a onfocus=”OnLink(this)” href=”]]></HTML><URL/>
<HTML><![CDATA[” ONCLICK=”GoToLink(this);return false;” target=”_self”>]]>
</HTML>
<Column HTMLEncode=’TRUE’ Name=”Title” Default=’$Resources:core,NoTitle;’>
</Column>
<HTML><![CDATA[</a>]]></HTML>
<IfNew>
<HTML><![CDATA[<IMG SRC=”/_layouts/[%=System.Threading.Thread.CurrentThread.CurrentUICulture.LCID%]/images/new.gif” alt=”]]></HTML>
<HTML>$Resources:core,new_gif_alttext</HTML>
<HTML><![CDATA[“>]]></HTML>
</IfNew>
</Else>
</IfEqual>
</DisplayPattern>

Got it ?You can write server-side code inside your display pattern :0 TERRIFIC
By the way I just discovered that when trying to answer your question but a little humble piece of advice to you is ” Love your 12 hive, Check out the OOB features, 12 Hive can really teach us a lot of cool stuff related to the UX

That was my answer and I’m waiting for him/her to mark it as answered LOL!
see you

Microsoft Innovation Day, Steve Ballmer Key note speaker

Microsoft Egypt is organizing Microsoft innovation day 22 April 2009 where all the Egyptian Online and Offline communities will gather and deliver some technical sessions and of course one of the communities is Egypt SharePoint Users Group.
Me and Marwan Tarek ( MOSS MVP) will be representing Egypt SharePoint Users Group and Marwan will be presenting “jQuery and SharePoint”.
Steve Ballmer will be the key note speaker for the innovation day and he will answer the Users groups questions about Microsoft strategy.

Hey SharePoint, please do not to handle that exception for me, I’ll take care of it!

It’s a good practice to catch the UnauthorizedAccessException within your sharepoint code , it might be thrown if the logged in user does not have enought permissions to get some code execued in his security context .
However, the SharePoint platform handles this exception on the developer’s behalf by redirecting to _layouts/AccessDenied.aspx page .
To override this behavior, you can set the property SPSecurity.CatchAccessDeniedException to false, telling SharePoint “Please do not to handle the exception for me, I’ll take care of it“.

Example :
bool AccessDeniedflag = SPSecurity.CatchAccessDeniedException;
SPSecurity.CatchAccessDeniedException = false;
try
{
SPList list = web.GetList(web.ServerRelativeUrl+”/Lists/ListName”);
SPListItem item = list.Items[0];
item[“title”] = “ayman-elhattab.blogspot.com”;
item.Update();
}
catch(UnauthorizedAccessException ex)
{
// Redirect to your custom page
SPUtility.Redirect(“MyAccessDeniedPage.aspx”,SPRedirectFlags.RelativeToLayoutsPage,this.Context);
}
finally
{
SPSecurity.CatchAccessDeniedException = AccessDeniedflag;
}

Protecting SharePoint lists against deleteion!

That was a problem that we encountered in one of our projects. We needed to prevent some lists and libraries from being deleted even by site collection adminsitartors.

Actually there are two ways to do that :

1) Using Event Handlers
Firstly I though of extending SPListEventReceiver, I expected the presence of a function called “ListDeleting”. Unfortunately, there is no such thing as “ListDeleting”.

Ali Al-Kahki ( My ITWorx colleague ) came up with a very nice workaround listed below :

public override void ItemDeleting(SPItemEventProperties properties)
{
if (properties.ListItemId == 0)
{
properties.Cancel = true;
properties.ErrorMessage = “You are not allowed to delete the list”;
}
}

2) Using SPList.AllowDeletion

When I was exploring the SharePoint object model, I discovered a very interesting property in the SPList class. When setting this property to false, the “Delete list” option will not be displayed in the list settings page .Even if you try to delete the list programmatically an exception will be thrown as long as this property is set to false.

SPList list = web.GetList(web.ServerRelativeUrl+”/Lists/ListName”);
list.AllowDeletion = false;
list.update();

I prefer the second approach but I like Ali’s workaround 🙂

MSDN Social Forums : Conversation about using resources in features definition files!

This morning, i was surfing the SharePoint forum @ msdn social forums searching for some stuff. A guy there was asking how he could globalize his features definition files ( removing language specific contenrs ) and obtaining those values dynamically from resources files. His company was developing a language pack for a service they offer built on MOSS 2007.
I answered his question , he marked it as answered so I just thought of sharing this conversation with you.
http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c58cf229-b219-4491-b25d-c97c3305901a/#837904e3-0a50-45f5-aca8-fc566407798b