Monday, 4 November 2013

Calling a code behind function from JQuery

function TestFunction(ddlType){
    
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "../Script Services/AutoComplete.asmx/SampleWebMethod",
        datatype: "JSON",
        parameters: { param1: ddlType },
        data: "{intTypeID: " + intTypeID + "}",
        success: function (response) {
            TestFunctionOnSuccess(ddlType, response.d);
        }
    });}

[WebMethod(EnableSession = true)]
[System.Web.Script.Services.ScriptMethod]
        public SampleObject[] SampleWebMethod(int? intTypeID)
        {
            DataTable dt = new DataTable();
            dt = ProjectBusinessLayer.Type.TypeSelect(intTypeID.Value);

            List<SampleObject> Details = new List<SampleObject>();
            foreach (DataRow dtrow in dt.Rows)
            {
                string TestID = dtrow["TestID"].ToString();
                string TestValue = dtrow["TestValue"].ToString();
                Details.Add(new SampleObject(TestID, TestValue));
            }
            return Details.ToArray();
        }

function TestFunctionOnSuccess(ddlType, lstTestDetails) {
// Do the functionality you need
}

Replace Header and Footer in an existing word document using Open xml sdk

i have a scenario where i have to replace header and footer of an existing document. i have found an article in msdn 
http://msdn.microsoft.com/en-us/library/office/cc546917.aspx i have used this code with modifications to replace both header and footer.
private void ReplaceHeaderAndFooter(WordprocessingDocument docx, byte[] byteHeaderArray, byte[] byteFooterArray)
        {
            docx.MainDocumentPart.DeleteParts(docx.MainDocumentPart.HeaderParts);
            docx.MainDocumentPart.DeleteParts(docx.MainDocumentPart.FooterParts);

            DocumentFormat.OpenXml.Packaging.HeaderPart headerPart = docx.MainDocumentPart.AddNewPart<HeaderPart>();
            DocumentFormat.OpenXml.Packaging.FooterPart footerPart = docx.MainDocumentPart.AddNewPart<FooterPart>();

            string strHeaderPartID = docx.MainDocumentPart.GetIdOfPart(headerPart);
            string strFooterPartID = docx.MainDocumentPart.GetIdOfPart(footerPart);

            using (MemoryStream stream = new MemoryStream())
            {
                stream.Write(byteHeaderArray, 0, byteHeaderArray.Length);
                using (WordprocessingDocument wdDocSource =
        WordprocessingDocument.Open(stream, true))
                {
                    DocumentFormat.OpenXml.Packaging.HeaderPart firstHeader = wdDocSource.MainDocumentPart.HeaderParts.FirstOrDefault();

                    if (firstHeader != null)
                    {
                        headerPart.FeedData(firstHeader.GetStream());
                    }
                }

            }

            using (MemoryStream stream = new MemoryStream())
            {
                stream.Write(byteFooterArray, 0, byteFooterArray.Length);
                using (WordprocessingDocument wdDocSource = WordprocessingDocument.Open(stream, true))
                {
                    DocumentFormat.OpenXml.Packaging.FooterPart firstFooter = wdDocSource.MainDocumentPart.FooterParts.FirstOrDefault();

                    if (firstFooter != null)
                    {
                        footerPart.FeedData(firstFooter.GetStream());
                    }
                }

            }

            IEnumerable<DocumentFormat.OpenXml.Wordprocessing.SectionProperties> sections = docx.MainDocumentPart.Document.Body.Elements<DocumentFormat.OpenXml.Wordprocessing.SectionProperties>();

            foreach (var section in sections)
            {
                section.RemoveAllChildren<DocumentFormat.OpenXml.Wordprocessing.HeaderFooterReferenceType>();

                section.PrependChild<DocumentFormat.OpenXml.Wordprocessing.HeaderReference>(new DocumentFormat.OpenXml.Wordprocessing.HeaderReference() { Type = DocumentFormat.OpenXml.Wordprocessing.HeaderFooterValues.Default, Id = strHeaderPartID });
                section.PrependChild<DocumentFormat.OpenXml.Wordprocessing.FooterReference>(new DocumentFormat.OpenXml.Wordprocessing.FooterReference() { Type = DocumentFormat.OpenXml.Wordprocessing.HeaderFooterValues.Default, Id = strFooterPartID });
            }
        }

If you use merge fields in the document header or footer you will get trouble in replacing the header or footer. In that case you have to replace the below

DocumentFormat.OpenXml.Packaging.FooterPart firstFooter = wdDocSource.MainDocumentPart.FooterParts.FirstOrDefault();

With

DocumentFormat.OpenXml.Packaging.FooterPart firstFooter = wdDocSource.MainDocumentPart.FooterParts.ToList()[1];