Saturday, 14 February 2015

Add item to sharepoint apps using napa office 365 clientWebPart

Share it Please
1.Create a Custom List Name as "Emp" and Create a below Column Name


2.Paste the below code in ClientWebpart.aspx

<%-- The following 4 lines are ASP.NET directives needed when using SharePoint components --%>
<%@ Page Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" language="C#" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<!-- The following tells SharePoint to allow this page to be hosted in an IFrame -->
<WebPartPages:AllowFraming runat="server" />

<html>
<head>
<!-- The following scripts are needed when using the SharePoint object model -->
<script type="text/javascript" src="../Scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="/_layouts/15/MicrosoftAjax.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>

<!-- Add your CSS styles to the following file -->
<link rel="Stylesheet" type="text/css" href="../Content/App.css" />

<script type="text/javascript">
'use strict';

// Set the style of the client web part page to be consistent with the host web
(function () {
var hostUrl = '';
if (document.URL.indexOf('?') != -1) {
var params = document.URL.split('?')[1].split('&');
for (var i = 0; i < params.length; i++) {
var p = decodeURIComponent(params[i]);
if (/^SPHostUrl=/i.test(p)) {
hostUrl = p.split('=')[1];
document.write('<link rel="stylesheet" href="' + hostUrl + '/_layouts/15/defaultcss.ashx" />');
break;
}
}
}
if (hostUrl == '') {
document.write('<link rel="stylesheet" href="/_layouts/15/1033/styles/themable/corev15.css" />');
}
})();
</script>
</head>

<body>


<table align="center">
<tr>
<td>
<label>First Name</label>
</td>
<td>
<input id="firstname" type="text" />
</td>
</tr>
<tr>
<td>
<label>Last Name</label>
</td>
<td>
<input class="lastname type="text" />
</td>
</tr>
<tr>
<td>
<label>Location</label>
</td>
<td>
<input class="Location type="text" />
</td>
</tr>
<tr>
<td>
<input type="button" value="Save" onclick="SetEmpData();" />

</td>
</tr>
</table>
  
</body>
<script type="text/javascript">
    var context;
    var web;
    var list;
    var listitem;

  
    function SetEmpData()
    {
            context = SP.ClientContext.get_current();
            var url = "https://sharepoint22.sharepoint.com/sites/DevSite";
            var parentCtx = new SP.AppContextSite(context, url);
            web = parentCtx.get_web();
            context.load(web);
            list = web.get_lists().getByTitle('Emp');
            var itemCreateInfo = new SP.ListItemCreationInformation();
            var listItem = list.addItem(itemCreateInfo);
listItem.set_item("Title","Mr");
            listItem.set_item("FirstName", $('#firstname').val());  
            listItem.set_item("LastName", $('.lastname').val());  
listItem.set_item("Location", $('.Location').val());
            listItem.update();
            context.load(listItem);
            context.executeQueryAsync(
                    Function.createDelegate(this, this.onQuerySucceeded),
                    Function.createDelegate(this, this.onQueryFailed)
            );
        }

    function onQuerySucceeded(sender, args) {
        alert('Item Added SuccessFully');
    }

    function onQueryFailed(sender, args) {
        alert(args.get_message());
      
    }

</script>


</html>
 


3.output 




No comments:

Post a Comment

Followers

Follow The Author