locked
How to copy item to a new item then edit it RRS feed

  • Question

  • SharePoint 2013.

    My problem is i need to make a button to copy a item to new item and then the page can automatically go to the edit page.

    I've created  a workflow to copy a item,but how can  i take them to the new item to edit.

    The workflow always go back to homepage.

    Monday, February 27, 2017 12:23 AM

All replies

  • Copy the workflow starting page url and create a custom link by adding the source parameter to the link and the workflow will return to a particular page. 

    Example:

    https://<your_site>/wfsvc/80a911570d0548ff83adafde05bce256/WFInitForm.aspx?TemplateID={2037d6a9-67e0-4c22-6484-3971b90b0e85}&WF4=1&Source=<your_page_url>




    Avijit Sur

    Monday, February 27, 2017 10:05 AM
  • Hi

    Please refer below link which can help you to step by step copy a item in to new lst

    1. http://www.go4sharepoint.com/Forum/sharepoint-designer-workflow-copy-9661.aspx

    Please Mark Answer or post as HELP Full if it help to resolved your issue.

    Monday, February 27, 2017 11:09 AM
  • Another option is to use CSOM with JQuery with the following steps:

    1. Open a notepad and copy the follwoing script into it and save as .txt or .html the file.

    <script type="text/javascript" src="/test/Documents/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
    	function getListItem(webUrl, listName,listItemId, complete, failure) {
    		console.log(webUrl + "/_api/web/lists/getbytitle('" + listName + "')/items(" + listItemId + ")")
    	    $.ajax({
    	        url: webUrl + "/_api/web/lists/getbytitle('" + listName + "')/items(" + listItemId + ")",
    	        method: "GET",
    	        headers: { "Accept": "application/json; odata=verbose" },
    	        success: function (data) {
    	            complete(data.d); 
    	        },
    	        error: function (data) {
    	            failure(data);
    	        }
    	    });
    	}
    	
    	
    	
    	function createListItem(webUrl,listName, itemProperties, complete, failure) 
    	{    
    	    $.ajax({       
    	       url: webUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",   
    	       type: "POST",   
    	       processData: false,  
    	       contentType: "application/json;odata=verbose",
    	       data: JSON.stringify(itemProperties),
    	       headers: {   
    	          "Accept": "application/json;odata=verbose",
    	          "X-RequestDigest": $("#__REQUESTDIGEST").val()
    	       }, 
    	       success: function (data) {           
    	           complete(data.d);  
    	           console.log(data.d); 
    	           newItemID = data.d.ID;
    				if(newItemID != 0) {
    					var redirectUrl = "/test/Lists/Test2/EditForm.aspx?ID=";
    					redirectUrl = redirectUrl + newItemID;
    					window.location = redirectUrl;
    				}
    	       },      
    	       error: function (data) {            
    	           failure(data.responseJSON.error);    
    	       }  
    	    });
    	} 
    	
    	
    	
    	function copyListItem(webUrl,sourceListName,targetListName,listItemId,complete,failure)
    	{
    	    getListItem(webUrl,sourceListName,listItemId,
    	    function(sourceItem){
    	        //prepare item
    	        var copyItem = {};
    	        for (var propertyName in sourceItem) {
    	          if (sourceItem.hasOwnProperty(propertyName)) {
    	              if(sourceItem[propertyName] != null && sourceItem[propertyName].hasOwnProperty('__deferred'))  //exclude deffered objects 
    	              {
    	                  continue;
    	              }
    	              if(propertyName == 'GUID' || propertyName == 'ID' || propertyName == 'Id')   
    	              {
    	                 continue;
    	              }                  
    	              if(propertyName == '__metadata') {
    	                   var targetItemType = GetItemTypeForListName(targetListName);
    	                   copyItem['__metadata'] = { 'type': targetItemType };
    	              }
    	              else {
    	                  copyItem[propertyName] = sourceItem[propertyName]; 
    	              }    
    	          }
    	        }
    	
    	        createListItem(webUrl,targetListName,copyItem,function(targetItem){
    	            complete(sourceItem,targetItem);
    	        },failure);
    	    },
    	    failure);   
    	}
    	
    	
    	function GetItemTypeForListName(name) {
    	    var itemType = "SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";
    	    var encItemType = itemType.replace(' ','_x0020_');
    	    return encItemType;
    	}
    	function btnClick() {
    		var sourceListName = 'Test1';
    		var sourceListItemId = 1;
    		var targetListName = 'Test2';
    		var newItemID = 0;
    		
    		console.log(_spPageContextInfo.webAbsoluteUrl);
    		copyListItem(_spPageContextInfo.webAbsoluteUrl,sourceListName,targetListName,sourceListItemId,printInfo,logError);
    	}
    	function printInfo(sourceItem,targetItem)
    	{
    		var sourceListName = 'Test1';
    		var targetListName = 'Test2';
    
    	    console.log(String.format('List Item has been copied from {0} to {1}',sourceListName,targetListName));  
    	}
    	
    	function logError(sender,args)
    	{ 
    	    console.log(args.get_message());
    	}
    </script>
    <input type="button" title="copyItem" value="Copy Item" onclick="btnClick();" >


    2. Find the followng variables in the page and replace with actual value:

    In btnClick():

    var sourceListName = 'Test1';
    var sourceListItemId = 1;
    var targetListName = 'Test2';

    In createListItem():

    var redirectUrl = "/test/Lists/Test2/EditForm.aspx?ID=";
    

    3. Upload jquery library file in the site and replace the url in the script

    4. Upoad the file in a library of the site and copy the file link.

    5. Add a content editor webpart the page and paste the copied script file link to the following webpart property:

    You can also use the same script with a twick as custom ribbon item and achieve the desired solution.


    Avijit Sur

    Monday, February 27, 2017 12:43 PM