Wednesday, 21 December 2016

How to add or remove fields in SharePoint list's new item form?


1. Go to advanced list setting.
2. Enable management of content types.
3. Go to the default content type and add or remove the columns.
4. Browse this list in SharePoint designer and create a new item form with the above content type.
5. Set the above form as default form.

Note: The same is applicable on edit and display item form as well.

SharePoint Designer: Embedding Image in Workflow Mailer


1. Create a Pictures Library in SharePoint.
2. Add the required image in the library and copy the image URL.
3. Create workflow in SP Designer.
4. Create a workflow variable with datatype as string.
5. Assign variable following value:
<img src="ImageURL" height="500" />
6. Place the above variable in the workflow mailer where image should be loaded. 

Thursday, 8 December 2016

SharePoint List Item: How to programmatically set value to a lookup column?


If you try to assign directly like below then you will get an error with message saying "Invalid data has been used to update the list item. The field you are trying to update may be read only".

newItem["Resource_x0020_ID"] = "john@company.com";

Below is the correct way to assign value to a lookup column:

SPListItem newItem = list.AddItem();

var empID = web.Lists["Resource Master"].GetItemById(Convert.ToInt32(101));

newItem["Resource_x0020_ID"] = new SPFieldLookupValue(empID.ID, empID.Title);

newItem.Update();




SharePoint Application Page: How to export DataTable data to Excel file using ClosedXML?


Installing ClosedXML in your project (VS)

Go to Tools > Library Package Manager > Manage NuGet Packages..

Search for ClosedXML and install it. This will install DocumentFormat.OpenXML as well for you.

Reference to above should be added under References automatically. If not then you can add them manually.

Code:

using DocumentFormat.OpenXml;
using ClosedXML.Excel;
using System.Xml;

using System.IO;

protected void Page_Load(object sender, EventArgs e)
        {
//So that page does not stop responding after generating Excel file (Response.End())
btnExportExcel.OnClientClick = "_spFormOnSubmitCalled = false;_spSuppressFormOnSubmitWrapper=true;";
}


protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = new DataSet();
                //Call method which returns DataTable
                ds.Tables.Add(GetDataTable());
                ExportToExcel(ds);
            }
            catch (Exception ex)
            {
                lblMessage.Text = "Error: " + ex.Message;
            }
        }


protected void ExportToExcel(DataSet ds)
        {
            XLWorkbook wb = new XLWorkbook();
            DataTable dt = ds.Tables[0];
            wb.Worksheets.Add(dt);

            string fileName = Server.UrlEncode("Report" +  "_" + DateTime.Now.ToShortDateString() + ".xlsx");
            MemoryStream stream = (MemoryStream)GetStream(wb);

            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
            Response.ContentType = "application/vnd.ms-excel";
            Response.BinaryWrite(stream.ToArray());
            Response.Flush();
            Response.End();
        }


        public Stream GetStream(XLWorkbook excelWorkbook)
        {
            Stream fs = new MemoryStream();
            excelWorkbook.SaveAs(fs);
            fs.Position = 0;
            return fs;
        }