Search This Blog

Friday 1 June 2012

Application Page in SharePoint Model Dialog Box - Part II

Hi All,

In my previous post Application Page in SharePoint Model Dialog Box we have discussed the various issues i got from application pages. Here I am going to discuss how to deal with List Field settings in LFI (ListFieldIterator).

Scenario:
I have two fields (Title, Description) in my list; I want to add/update items to this list through application page, where I used ListFieldIterator. In LFI the Title field wants to be mandatory for a case and it does not mandatory for another case. I have used single application page for both cases. So I want to play with list field setting to achieve this. I tried with various approach, finally I decided to go with one of the following two ways.

Approach 1:

Get the title field and change the settings

Code snippet:
protected void Page_Init(object sender, EventArgs e)
        {
            using (SPSite osite = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb oweb = osite.OpenWeb())
                {
                    SPList olist = oweb.Lists.TryGetList("Job Aid Sections");
                    if (olist != null)
                    {
                        lfiUpdateJobAid.ListId = olist.ID;
   lfiUpdateJobAid.ControlMode = SPControlMode.New;
                        SPField oField = olist.Fields["Title"];
                        oField.Required = false;
   oweb.AllowUnsafeUpdates = true;
                        oField.Update();
                        oweb.AllowUnsafeUpdates = false;
                       
                    }
                }
            }
        }
Note: In this approach we have changed the list settings. There are changes to access multiple users concurrently.  So it leads the application to execute inconsistently. Refer Approach 2 for alternative

Approach 2:

protected void Page_Init(object sender, EventArgs e)
        {
            using (SPSite osite = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb oweb = osite.OpenWeb())
                {
                    SPList olist = oweb.Lists.TryGetList("Job Aid Sections");
                    if (olist != null)
                    {
                        lfiUpdateJobAid.ListId = olist.ID;
   lfiUpdateJobAid.ControlMode = SPControlMode.New;
                        lfiUpdateJobAid.Fields["Title"].Required = false;
                        lfiUpdateJobAid.Fields[""].ReadOnlyField = true;
                        lfiUpdateJobAid.ExcludeFields = "Title;#IsDeleted;#DisplayOrder";
                    }
                }
            }
        }

Note: This approach does not change the list settings.

Complete code:

Ascx
<script type="text/javascript"">
     function fnOpenUpdateSection(queryString, title, oldJobAidStatusID, jobAidID) {
         addSectionUri = "/_layouts/LisFiedIteratorResize/LFIApplPage.aspx";
         var options = { url: addSectionUri, width: 0, height: 0, showMaximized: true, title: "Test Page" };
         SP.UI.ModalDialog.showModalDialog(options);
         return false;
     }
 </script>
 <asp:Button ID="btnTest" runat="server" Text="Click" />

Ascx.cs
protected void Page_Load(object sender, EventArgs e)
        {
            btnTest.Attributes.Add("onclick", "javascript:return fnOpenUpdateSection()");
        }

        protected void btnTest_Click(object sender, EventArgs e)
        {
            //Response.Redirect("");
        }

Aspx
<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
<style type="text/css">
    .ms-long
    {
        width:50%;
    }
</style>
</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<table id="tblUpdateScript" runat="server" cellpadding="0" cellspacing="0" width="100%">
        <tr>
            <td>
                <SharePoint:ListFieldIterator ID="lfiUpdateJobAid" runat="server" ControlMode="New">
                </SharePoint:ListFieldIterator>
            </td>
        </tr>
        <tr id="trFileUpload" runat="server">
            <td valign="top">
               <asp:Label ID="lblAttachments" runat="server" CssClass="labelAttachment">Attachments</asp:Label>
            </td>
            <td>
                <asp:FileUpload ID="fuSectionAttachment" runat="server" />&nbsp;
                <asp:Label ID="lblAttach1Error" runat="server" CssClass="labelAttachmentError"></asp:Label><br />
                <asp:FileUpload ID="fuSectionAttachment1" runat="server" />&nbsp;
                <asp:Label ID="lblAttach2Error" runat="server" CssClass="labelAttachmentError"></asp:Label><br />
                <asp:FileUpload ID="fuSectionAttachment2" runat="server" />&nbsp;
                <asp:Label ID="lblAttach3Error" runat="server" CssClass="labelAttachmentError"></asp:Label>                               
            </td>      
        </tr>
        <tr>
            <td>
                &nbsp;
            </td>
        </tr>
        <tr>
            <td valign="top" colspan="2">
                <asp:LinkButton ID="lbnSave" CommandName="Save" runat="server" OnClick="btnSave_Click">
                    <img id="Img1" src="/Style%20Library/images/save.png" align="absMiddle" runat="server"
                        alt="Save" />
                </asp:LinkButton>&nbsp;&nbsp;               
                    <img id="Img2" src="/Style%20Library/images/cancel.png" align="absMiddle" runat="server"
                        alt="Cancel" onclick="javascript:SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.cancel, 'Cancelled clicked');" />               
            </td>
        </tr>
        <tr>
            <td valign="top" colspan="2">
                <asp:Label ID="lblErrorMessage" runat="server"></asp:Label>
            </td>
        </tr>
    </table>
</asp:Content>

Aspx.cs
protected void Page_Init(object sender, EventArgs e)
        {
            using (SPSite osite = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb oweb = osite.OpenWeb())
                {
                    SPList olist = oweb.Lists.TryGetList("Job Aid Sections");
                    if (olist != null)
                    {
                        lfiUpdateJobAid.ListId = olist.ID;
                        lfiUpdateJobAid.ControlMode = SPControlMode.New;
                        lfiUpdateJobAid.ExcludeFields = "Title;#IsDeleted;#DisplayOrder";
                      
                        // Approach 1
                        SPField oField = olist.Fields["Title"];
                        oField.Required = false;
                        oweb.AllowUnsafeUpdates = true;
                        oField.Update();
                        oweb.AllowUnsafeUpdates = false;

                        //Approach 2
                        lfiUpdateJobAid.Fields["Title"].Required = false;
                        lfiUpdateJobAid.Fields[""].ReadOnlyField = true;
                       
                    }
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            lbnSave.Click += new EventHandler(btnSave_Click);
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //if (Page.IsValid)
            //{
                BaseFieldControl fieldControl = GetFieldControlByName("Section Content");
                if (Convert.ToString(fieldControl.Value.ToString().Trim()) == string.Empty)
                {
                    fieldControl.ErrorMessage = " ";
                    fieldControl.IsValid = false;
                }
                else
                {

                    lfiUpdateJobAid.ListItem["JobAidID"] = 1;
                    lfiUpdateJobAid.ListItem["SectionType"] = 0;
                    lfiUpdateJobAid.ListItem["IsDeleted"] = 0;

                    lfiUpdateJobAid.Web.AllowUnsafeUpdates = true;
                    lfiUpdateJobAid.ListItem.Update();
                    lfiUpdateJobAid.Web.AllowUnsafeUpdates = false;


                    Response.Write("<script language='javascript'>window.parent.location.href = window.parent.location.href</script>");
                }
            //}
        }
        protected BaseFieldControl GetFieldControlByName(String fieldNameToFind)
        {
            BaseFieldControl baseField = null;
            foreach (Control control in Page.Validators)
            {
                if (control is BaseFieldControl)
                {
                    baseField = control as BaseFieldControl;
                    String fieldName = baseField.FieldName;
                    if ((fieldName == fieldNameToFind))
                    {
                        return baseField;
                    }
                }
            }
            return baseField;
        }



No comments:

Post a Comment