Thursday, 19 November 2015

Event receiver not attached on production

Event receiver is working fine for all custom lists in development environment.
Issue: When i deploy the same event receiver solution file in production server, it is not firing.
Perform below powershell script
$spWeb = Get-SPWeb -Identity "http://npduat:7001/"
$spList = $spWeb.Lists["DocumentTrackingEmailNotification"]
$spEventReceiver = $spList.EventReceivers.Add()
$spEventReceiver.Assembly = "BFL.PPAP.Webparts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=56fcab577ff38308"
$spEventReceiver.Class = "BFL.PPAP.Webparts.EventReceivers.DocumentTrackingEmailNotificationReceiver.DocumentTrackingEmailNotificationReceiver"
$spEventReceiver.Name = "DocumentTrackingEmailNotificationReceiverItemAdded"
$spEventReceiver.Type = "ItemAdded"
$spEventReceiver.SequenceNumber = 1000
$spEventReceiver.Synchronization = 1
$spEventReceiver.Update()

Refer link


Tuesday, 17 November 2015

Enable Save As option in Sharepoint for browser.

Will not see "Save As" option when we download any document from sharepoint document library. It  will be shown when we click on "Download a copy" in SharePoint ribbon.

Refer below link for enable "Save As" option for browser.
Link

Using Above link we can enable a "Save As" option in IE but it will prompt for user credentials i.e. not work for anonymous users.

For anonymous User , we need to write code as below.

Add Application page and write below code.

Note : Class should inherit with UnsecuredLayoutsPageBase .

 public partial class DownloadFile : UnsecuredLayoutsPageBase
    {
        protected override bool AllowAnonymousAccess
        {
            get
            {
                return true;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    using (SPSite site = new SPSite(SPContext.Current.Site.Url, SPUserToken.SystemAccount))
                    {
                        string path = Request.QueryString["path"];
                        string documentId = Request.QueryString["DocumentId"];
                        string fileName = string.Empty;

                        if (path != null && path.Length > 0)
                        {
                            int lastIndex = path.LastIndexOf("/");
                            fileName = path.Substring(lastIndex + 1, (path.Length - lastIndex - 1));

                            byte[] binfile = SPContext.Current.Web.Lists["Tools"].GetItemById(Convert.ToInt32(documentId)).File.OpenBinary(SPOpenBinaryOptions.None);

                            Response.Clear();
                            Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
                            Response.BinaryWrite(binfile);
                            Response.Flush();
                            Response.End();
                        }
                    }
                }
                catch (Exception ex)
                {
                    // do nothing;
                }
            }
        }


    }