Archive

Archive for the ‘Share Point’ Category

SharePoint: Get content type, which is associated with the current request

December 29th, 2010 No comments

     In my project I’ve created custom display/edit/new forms for every usable list. The easiest way to get a SPContentType-instance, which is connected with the current Page, is a property SPContext.Current.ListItem.ContentType. Sometimes, I have to get the current content type inside OnInit-method of page lifecycle. If we edit or display an existed list item (edit/display-forms), we can do it easily, because SPContext.Current.ListItem is already defined and accessible. But, if we create a new list item (new-form), we don’t have a valid SPContext.Current.ListItem inside OnInit, because none is created yet. The following table demonstrates when (starting from what method of page lifecycle) SPContext.Current.ListItem.ContentType is already accessible:

Form

Accessible starting from

Edit OnInit
Display OnInit
New OnLoad

     To get the current content type inside OnInit of New-form, at first we need to extract ContentTypeId from query-string of Page.Request, then having ContentTypeId we receive the instance of SPContentType. It should be noted that SPContext has an internal method GetContentTypeThroughQueryString, which provides SPCOntentType-instance based on id of content type in the query-string.

     Of course, we can invoke this internal method through .Net Reflexion, but I prefer rewriting such methods, when it’s possible. So, now I have my own GetContentTypeThroughQueryString method:

protected SPContentType GetContentTypeThroughQueryString()
{
    string cntTypeIdStr = HttpContext.Current.Request.QueryString["ContentTypeId"];
    if (!string.IsNullOrEmpty(cntTypeIdStr))
    {                
        try
        {
            SPContentType spContentType = null;
            if (cntTypeIdStr.StartsWith("0x"))
            {
                SPContentTypeId contentTypeId = new SPContentTypeId(cntTypeIdStr);
                spContentType = SPContext.Current.List.ContentTypes[contentTypeId];
                if (spContentType == null)
                    spContentType = SPContext.Current.List.ContentTypes[SPContext.Current.List.ContentTypes.BestMatch(contentTypeId)];
            }
            else
            {
                using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture))
                {
                    int num = int.Parse(cntTypeIdStr, writer.FormatProvider);
                    spContentType = SPContext.Current.List.ContentTypes[num];
                }
            }

            return spContentType;
        }
        catch{}
    }
    return null;
}

     And the result method, that returns the current content type associated with the current request, is

protected SPContentType GetCurrentContentType()
{
    if (SPContext.Current.ListItem != null && SPContext.Current.ListItem.ContentType != null)
        return SPContext.Current.ListItem.ContentType;

    // try to get content type through query string
    return GetContentTypeThroughQueryString();
}

     GetCurrentContentType can be invoked inside any method of page lifecycle, even inside OnInit.

Related posts:

SharePoint: Add ‘onchange’-attribute to DropDownChoiceField

December 2nd, 2010 No comments

To change the values of controls logically connected to DropDownChoiceField, I decided to add a client-side JavaScript handler that fires when selected value of DropDownChoiceField is changed. Unfortunately, DropDownChoiceField doesn’t have a habitual collection of attributes. But DropDownChoiceField is a template control and contains DropDownList inside, which allows to add attributes. We need to extract DropDownList from DropDownChoiceField and process it. I think the better way to do that is the Page_PreRender or similar methods, because all child controls of DropDownChoiceField are already created by this moment. According to Reflector, the id of DropDownList is “DropDownChoice”. To get control I use recursive FindControl function.

protected DropDownChoiceField choiceField;
//..............
protected void Page_PreRender(object sender, EventArgs e)
{
    DropDownList ddl = FindControlRecursive(choiceField, "DropDownChoice") as DropDownList;
    if (ddl != null)
        ddl.Attributes.Add("onchange", "javascript:alert('Hello')");
}
public static Control FindControlRecursive(Control root, string id)
{
    if (root.ID == id)
        return root;

    foreach (Control c in root.Controls)
    {
        Control t = FindControlRecursive(c, id);
        if (t != null)
            return t;
    }

    return null;
}
Related posts:
Categories: ASP.NET, Share Point Tags: ,