Friday, December 27, 2019

Get attachments in list items JSlink (REST API)

In this article I’ll give an example of how to use REST API and JSlink to make a request to attachments in list items which will be useful for working with current and future development projects on the MS SharePoint server (2013, 2016, 2019, Online) platform.
function AttachmentFiledTemplate(ctx){ 
    var listItem = ctx.CurrentItem;
    var listTitle = ctx.ListTitle;
    var spHostUrl = ctx.HttpRoot;
    var itemId = listItem.ID;
    context = ctx;
    var queryUrl = spHostUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items('" + itemId + "')/AttachmentFiles";
    $.ajax({
    url: queryUrl,
        headers: { 
            "accept": "application/json; odata=verbose",
        },
        method: "GET",
         success: function(data){
            $.each(data.d.results, function(){
                //url attachments in list Items
                var attachmenturl = this.ServerRelativeUrl;
                console.log(attachmenturl);
            })
        },
        error: function ajaxError(response){
        alert(response.status + ' ' + response.statusText);
        }
    });
}

We implement using the REST API the name of the attached files in the list items (Example):
(function () {
    var linkFiledContext = {};
    linkFiledContext.Templates = {};
    linkFiledContext.Templates.Fields = {    
        "Attachments": { "View": AttachmentsFiledTemplate }
    };
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(linkFiledContext);

})();

function AttachmentsFiledTemplate(ctx) {
    var itemId = ctx.CurrentItem.ID;
    var listName = ctx.ListTitle;       
    return getAttachments(listName, itemId);
}

function getAttachments(listName,itemId) {  
    var url = _spPageContextInfo.webAbsoluteUrl;
    var requestUri = url + "/_api/web/lists/getbytitle('" + listName + "')/items(" + itemId + ")/AttachmentFiles";
    var str = "";
    $.ajax({
        url: requestUri,
        type: "GET",
        headers: { "ACCEPT": "application/json;odata=verbose" },
        async: false,
        success: function (data) {
            for (var i = 0; i < data.d.results.length; i++) {
                str += "<a href='" + data.d.results[i].ServerRelativeUrl + "'>" + data.d.results[i].FileName + "</a>";
                if (i != data.d.results.length - 1) {
                    str += "<br/>";
                }                
            }          
        },
        error: function (err) {
        }
    });
    return str;
}

Result:
Happy Coding!

Thursday, December 12, 2019

Server Error '/' Application: Could not load file or assembly or one of its dependencies. The system cannot find the file specified.

Hello everyone, let's talk about the error that comes out after adding a new assembly to the project, for example via NuGet Package in Visual studio (Template Project: SharePoint 2016 - Visual Web Part).
Let's go step by step:
1. Open Visual studio on MS SharePoint server 2016 (On-Premises) and create new project - SharePoint 2016 - Visual Web Part,
2. Add to URL and choise "Deploy as a farm solution",
3. Add simple code (example: if (!IsPostBack) {//your simple code}) in the void Page_Load,
4. Further we need to add assembly (example: "TemplateEngine.Docx") for the project in NuGet Package -> "Tools" -> "NuGet Package manager" -> "Package manager Console", -> "Install-Package TemplateEngine.Docx -Version 1.1.4",
5. We are convinced that the assembly was added and displayed in "Reference", add simple code to example and weel see is not errors in "Error List".
6. "Build" -> "Rebuild Solution" -> "Deploy Solution".
7. Add our solution in the Page and weel see the next error:


This behavior at first glance seems strange, we added the assembly through the NuGet Package console, it would seem that when adding the assembly, it is registered in the Reference and then in all the project properties. But the error that the assembly file is missing is still there. So how to fix it quickly and most importantly correctly, again we go step by step:
1. In our project there is folder "Package" in which there is file "Package.package" a file associated with Features, where the paths for assemblies in the current project are contained, open it and immediately go to the "Advanced" tab and "Add Existing Assembly":


2. The "Add Existing Assembly" window has opened, where you can add the assembly, follow the path to our project to the \bin\Debug folder and find our assembly - "TemplateEngine.Docx.dll".
3. Choose "WebApplication" and add new item in "Safe Control": "Namespace": TemplateEngine.Docx.dll, "Assembly Name": TemplateEngine.Docx.dll.


4. We see our record and again collect our project and publish it on the server: "Build" -> "Rebuild Solution" -> "Deploy Solution".


5. Refresh Page!!!.

Happy Coding!