Thursday 28 May 2015

Execute Javascript or JQuery after the Entire page has loaded in SharePoint

If you're anything like me and are working with a lot of Javascript/JQuery within Sharepoint. You will eventually need to run a script after the page loads. So you could add defer="defer" to your script tag, which sometimes works and sometimes doesn't.

You might be tempted to window.onload = yourFunction(). But this will break the way Sharepoint load's it's pages. As Sharepoint also needs to do a lot of things after the page loads.

So the solution is:

Javascript:
window.attachEvent("onload", yourFunction);

So instead of overriding the onload event. You are now adding an event to the queue. Simply replace the "yourFunction" bit with your function name.

Eg:

window.attachEvent("onload", HideBranding);

function HideBranding() {
.....
.....
}

JQuery:

$(window).load(function() {
....
....
});

Thursday 9 April 2015

Create Document Set in SharePoint 2010 Sandbox solution programmatically

Today I came across a requirement in which I had to create a Document Set in a Sandbox solution programmatically. As DocumentSet.Create function is not supported in Sandbox solution I had to think for another alternative and worked on a little trick and that worked!!
First I created a  folder and then converted that folder content type to Document Set.

You have to first add/enable the Document Set Content Type to the document library to which you are going to create the document set.

Below is the code snippet:

protected void AddToSharePoint(MemoryStream memStream, string fileName)
        {

            using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
            {
                using (SPWeb web = site.OpenWeb())
                {

                    //Get the document library object
                    SPList docLib = web.Lists["Documents"];
                    SPContentType docSetName = docLib.ContentTypes["Document Set"];
                    if (docLib != null)
                    {
// Create Folder
                        SPListItem folder = docLib.Folders.Add(docLib.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, "DocSet_" + DateTime.Now.ToString("yyyyMMddHHmmss"));
// Assign Document Set Content Type
                        folder["ContentTypeId"] = docSetName.Id;
                        folder.Update();
//Add a document to the newly created Document Set
                        SPFile file = folder.Folder.Files.Add(fileName, memStream.ToArray(), true);
                        file.Update();
                    }
                }
            }
        }

Wednesday 25 March 2015

SharePoint 2013: How to get 'Sign in as Different User' option?

The 'Sign in as Different User' has been removed by default, but it can be easily added.

There are a three options to do it:

1. Open your IE browser by pressing shift key and right clicking on it and select 'as another user'.
2. Navigate to the url <http://mysite>/_layouts/closeConnection.aspx?loginasanotheruser=true
3. Perform the following steps to add it to the user menu:

a      Locate and then open the following file in a text editor: C:\Program Files\Common Files\Microsoft Shared\Web Server
Extensions\15\TEMPLATE\CONTROLTEMPLATES\Welcome.ascx

b.      Add the following element before the existing "ID_RequestAccess" element:
<SharePoint:MenuItemTemplate runat="server" ID="ID_LoginAsDifferentUser" Text="<%$Resources:wss,personalactions_loginasdifferentuser%>" Description="<%$Resources:wss,personalactions_loginasdifferentuserdescription%>" MenuGroupId="100" Sequence="100" UseShortId="true" />

c.      Save the file.