Friday 27 May 2016

Import data from a CSV file into a SharePoint Online using JSOM

Import bulk data from CSV to SharePoint List in O365.



Recently I have created a tool which will help you to import n number of csv data to SharePoint List this tool will help you to import data in SharePoint online and OnPrem.


SharePoint Online 


1. Click on Upload CSV -

 2. Select your CSV file .



3. Final Result


Kindly drop your comments if this is useful for you.

Thanks,
Pankaj Srivastava




Wednesday 4 May 2016

Open Provider hosted Apps in SharePoint Model Dialog without using a Custom Action

Hi All ,

If you are new to SharePoint Provider hosted Apps and you want to open your Provider-hosted Apps in a SharePoint Model dialog then Microsoft suggests you should use a custom Action to achieve this functionality.


I was struggling a lot and I didn't want to use a custom action to open a Provider hosted App in a Model dialog, so I dug deeper into the custom action functionality. I found the Js function that the custom action uses internally use to open Provider hosted App in Model Dialog. I finally achieved success without using Custom Action and I have used below script to open my Provider hosted in Model dialog on click on anchor tag.

<a href="#" onclick="javascript:LaunchApp('13f91bd6-b83d-85cb-921a-v418368ae07a', 'i:0i.t|ms.sp.ext|f4df267v-3sa2-6406-a46c-1ba3b94d0715@1d586085-556e-2af5-244b-31c7481vc6a3', 'https:\u002f\u002fxyz.azurewebsites.net\u002fPages\u002fDefault.aspx?{StandardTokens}', {width:900,height:800,title:'Create Request'});">Open My Provider hosted App</a>

And, it's working perfectly without using a custom action ... you can pass your Item Id as well to provider hosted App from SharePoint model dialog.
I hope it will help you a lot for any question and query get in touch with me .


Happy Coding !!!


Thanks,
Pankaj Srivastava

How to close a Modal Dialog from a Provider-Hosted App using code-behind

Hi Guys,

If you are facing a challenge while closing a modal dialog in a provider hosted app, using code behind then please use the script below.

How to close the Modal Dialog of a Provider-Hosted App using code Behind...

1. If you want to refresh your parent window after closing your dialog then use the script below:

  HttpContext.Current.Response.Write("<script language='JavaScript'>window.parent.postMessage('CloseCustomActionDialogRefresh', '*');</script>");

2. If you don't want to refresh your parent page after closing your dialog then use the script below:

HttpContext.Current.Response.Write("<script language='JavaScript'>window.parent.postMessage('CloseCustomActionDialogNoRefresh', '*');</script>");

I hope this will help you a lot...

Happy coding.... :)

Regards!!

Pankaj Srivastava

Open Provider hosted Apps in SharePoint Model Dialog Using Custom Action

Hi Guys,


If you are new to provider hosted App and you have requirement to open list item in your popup model dialog from SharePoint Online  as per Microsoft https://msdn.microsoft.com/en-us/library/office/jj163816.aspx  there is only one way to pass item id using custom action.  But I have found another tricks to open your model dialog without using custom Action and you can pass  item id from there as well.


So if you want to open model dialog the you need to add three property as below code

      HostWebDialog="true"
           HostWebDialogHeight="800"
           HostWebDialogWidth="850"
-----------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="6465cfae-e358-47ef-a41e-570f628318a4.openDialog"
                RegistrationType="List"
                RegistrationId="{$ListId:Lists/customlist;}"
                Location="EditControlBlock"               
               Sequence="399"
               Title="Create Freight Request"
             HostWebDialog="true"
           HostWebDialogHeight="800"
           HostWebDialogWidth="850">

    <UrlAction Url="https://xyz.azurewebsites.net/Pages/Default.aspx?{StandardTokens}" />
  </CustomAction>
</Elements>

Happy Coding !!


Retrieving all pageLayout in CSV from sharepoint site using Powershell

clear
filter Get-PublishingPages {
$pubweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($_)
$query = new-object Microsoft.SharePoint.SPQuery

$query.ViewAttributes = "Scope='Recursive'"
$pubweb.GetPublishingPages($query)
}
$FilePath="D:\\Pankaj\\PageAndLayouts.csv"

$str = Read-Host "Enter Site URL:"
if($str -eq $null )
{
Write-Host "Enter a valid URL”
return
}

$site = Get-SPSite -Identity $str
if($site -eq $null)
{
Write-Host "Enter a valid URL"
return
}

$allweb = $site.Allwebs
foreach($web in $allweb )
{
Write-Host $web.Title;
$web | Get-PublishingPages | select Uri, Title, @{Name='PageLayout';Expression={$_.Layout.ServerRelativeUrl}}|Export-CSV -Append $FilePath
}

Hiding a Site Content Link from Setting Icon in SharePoint 2013

Hello Everyone,

Recently I received a  requirement from client to hide Site Content Links from only one site page which 
comes along with Setting Wheels from Corner. I have tried a lot to achieve it from OOTB  but I did't get any solution,I have gone through many blogs and articles they are suggesting to make a changes in Master Page which does not make sense to do the changes for only one Page. so I have decided to use JQuery and REST to achieve my requirement. I hope this will help you a lot .






Please use below code to hide the site content Link

//get the current LoggedIn User Id
var userID=_spPageContextInfo.userId;
//Function to check If User Is Site Admin then then Site Content Link should be visible as it is if not then hide.
function CheckUserIsAdmin() {
$.ajax
({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetUserById(" + userID + ")",
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
dataType: "json",
async: true,
success: function (data) {
//if loggedIn user is not a site Admin then hide the site content
var Obj=data.d.IsSiteAdmin;
var element;
if(!Obj)
{
var element=$("#siteactiontd").children().children().children()[4];
element.remove();
}
}
});
}
</script>



Thanks,
Pankaj Srivastava

Working With Provider Hosted App SharePoint Online + Azure

If you are new to provider hosted App and you would like to create a new Provider hosted app in SharePoint Online this article will help you to creating a provider hosted app in SharePoint online + windows azure as hosting infrastructure.

Provider-hosted app includes components that are deployed and hosted outside the SharePoint farm. They are installed to the host web, but their remote components are hosted on another remote server that should not be a server in the SharePoint farm.

1. Before creating Application lets first create a fresh web Application  on azure to host a Application
Log on  to https://manage.windowsazure.com click on create webApp.


Enter your WebApp Name- Click on Create WebApp Button


Once Application get created click on Application and go to dashboard tab there will be an option to download Publishing Profile click and download that publishing profile.


Now Open VS – Create New ProjectChoose Apps For SharePoint – choose hosting option as Provider Hosted –
 



Now once your application gets created lets register your app to O365/SharePoint Online Site

1. Register Your App to SharePoint Online/Office365
Open AppRegNew.aspx  - https://abc.sharepoint.com/sites/site1/_layouts/15/appregnew.aspx


 Make sure you have copied client Id and Client Secret- now go to the VS solution open AppMenifest.xml file 


Also update the web.config file-


Now go to VS Right Click on host Web=> Click on publish=> Click on Deployment – select current Profile –NEW a new Window will appear to upload your publishing Profile that you  have downloaded from Azure.


Click On finish - Again a new window will appear that will ask to provider client secret and client Id


Now click to publish your app web Project


Once your app Web publish click  right click on Host Web to Package your .app file .



Once you click on Package app your .app file will be generated  now go to Catalog site upload your .app file





Last and final step - click on Site contents - Add an App- add your app to  SharePoint Online now you are done.

Hope this article will help you a lot..

feel free to drop your comments.

Happy Coding!!

Working with Remote Event Receivers in a Provider Hosted App


Hi All, I would like to share my experiences while working on provider-hosted App Remote Event Receiver. When I was working with Remote Event Receiver's for the first time I faced a lot of challenges. For example, I was not able to find why my Remote Event Receiver was not triggering and after a lot of investigation, I found the cause of the problem. If you are new to provider-hosted App Remote Event Receiver's this article will help you a lot.

1.  Right click on your HostWebProject add new Remote Event receiver - I am using here Item added event


2. Now as you can see your event receiver will be added to solution so once you will add event receiver one RemoteEventReciver1.svc file will be created in AppWeb Solution.


3. Once your RemoteEventReceiver has been added to solution, open your element.xml file update your  .svc Location URL


4.  .svc file will be created in your AppWebProject 



Now Write your code on RemoteEventReciver1.svc.cs

I have written my code on ProcessOnWayEvent that fires after an action occurs, such as after a user adds an item to a list (because I have chosen on ItemAdded)


 
Now your event receiver will be created and when you run "deploy" this will not work! I was facing exactly the same problem with my Event Receiver not triggering,  so the question is - how do we overcome from this issue? Okay, so lets move ahead.

Next , right-click on your HostWeb Project - double click on "Handle App Installed" (if you want to Perform something when your App Installed).


When "Handle App Installed" is "true" then it will generate a new AppEventReceiver.svc in the AppWebProject


Open AppEventReceiver.svc.cs file add below code


Now I have written my code on AppInstalled Event-  which will attached RemoteEventReceiver1 to My List
-------------------------------------------------

Now I have written my code on AppInstalled Event-  which will attached RemoteEventReceiver1 to My List
-------------------------------------------------
  private void HandleAppInstalled(SPRemoteEventProperties properties)
        {
            try
            {
                using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
                {
                    if (clientContext != null)
                    {
                        List olist = clientContext.Web.Lists.GetByTitle("TestCustom");
                        clientContext.Load(olist, o => o.EventReceivers);
                        clientContext.ExecuteQuery();
                        bool isReRExsist = false;
                        foreach (var receiver in olist.EventReceivers)
                        {
                            if (receiver.ReceiverName == "RemoteEventReceiver1")
                            {
                                isReRExsist = true;
                                break;
                            }
                        }
                        if (!isReRExsist)
                        {

                            string remoteUrl = "https://xyz.azurewebsites.net/Services/RemoteEventReceiver1.svc";
                            EventReceiverDefinitionCreationInformation eventReDefCreation = new EventReceiverDefinitionCreationInformation()
                            {

                                EventType = EventReceiverType.ItemAdded,
                                ReceiverAssembly = Assembly.GetExecutingAssembly().FullName,
                                ReceiverName = "RemoteEventReceiver1",
                                ReceiverClass = "RemoteEventReceiver1",
                                ReceiverUrl = remoteUrl,
                                SequenceNumber = 15000
                            };
                            olist.EventReceivers.Add(eventReDefCreation);
                            clientContext.ExecuteQuery();

                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }

        }
and detaching my EventReceiver from List when my app will uninstalled from site
private void HandleAppUnistalled(SPRemoteEventProperties properties)
        {
            using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
            {
                var list = clientContext.Web.Lists.GetByTitle("TestCustom");
                clientContext.Load(list);
                clientContext.ExecuteQuery();
                EventReceiverDefinitionCollection eventRColl = list.EventReceivers;
                clientContext.Load(eventRColl);
                clientContext.ExecuteQuery();
                List<EventReceiverDefinition> toDelete = new List<EventReceiverDefinition>();
                foreach (EventReceiverDefinition erdef in eventRColl)
                {
                    if (erdef.ReceiverName == "RemoteEventReceiver1")
                    {
                        toDelete.Add(erdef);
                    }
                }

                //Delete the remote event receiver from the list, when the app gets uninstalled
                foreach (EventReceiverDefinition item in toDelete)
                {
                    item.DeleteObject();
                    clientContext.ExecuteQuery();
                }
            }
        }

Now deploy your code it will work fine....

I hope my article will help you guys pls. drop your comment if you will face any issue kindly contact me.

Happy Coding !!!


Thanks,
Pankaj Srivastava