0% found this document useful (0 votes)
42 views5 pages

C# FileLogger and SharePoint Integration

The document outlines a technical test covering SharePoint, C#, and JavaScript. It includes steps for creating a Power Automate approval flow, fetching the latest news using PnP.js, retrieving document metadata in C#, and validating form data in JavaScript. Additionally, it provides examples for logging and updating status via an API in SharePoint.

Uploaded by

marku.jawadiana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views5 pages

C# FileLogger and SharePoint Integration

The document outlines a technical test covering SharePoint, C#, and JavaScript. It includes steps for creating a Power Automate approval flow, fetching the latest news using PnP.js, retrieving document metadata in C#, and validating form data in JavaScript. Additionally, it provides examples for logging and updating status via an API in SharePoint.

Uploaded by

marku.jawadiana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Technical Test: SharePoint, C#, and JavaScript

Section 1: SharePoint

Question 1: Power Automate Approval Flow

Steps to create the approval flow in Power Automate:


1. Trigger: "When a file is created" from the specific document library.
2. Add approval action for the Manager.
- Action: "Approve or Reject"
- Assign it to the Manager (static or dynamic email).
3. Condition to check Manager approval: If approved, continue; otherwise, end the flow.
4. Add approval action for the Director.
5. Condition to check Director approval: If approved, update the 'Status' field in SharePoint
to 'Approved', otherwise, end the flow.
6. Use "Update file" action to modify the 'Status' field.
7. Finish the flow by sending a notification to the requester.

Question 2: PnP.js to Display Latest 5 News

```javascript
import { sp } from "@pnp/sp/presets/all";

async function fetchLatestNews() {


try {
const news = await sp.web.lists
.getByTitle("Noticias")
.items
.select("Title", "Created")
.orderBy("Created", false)
.top(5)
.get();

console.log("Latest news:", news);


return news;
} catch (error) {
console.error("Error fetching news:", error);
}
}

fetchLatestNews();
```

Section 2: C#

Question 1: GetDocumentMetadata

```csharp
using Microsoft.SharePoint.Client;
using System;
using System.Security;

class SharePointExample
{
public static void GetDocumentMetadata(string siteUrl, string username, string
password)
{
try
{
SecureString securePassword = new SecureString();
foreach (char c in password) securePassword.AppendChar(c);

using (var context = new ClientContext(siteUrl))


{
context.Credentials = new SharePointOnlineCredentials(username,
securePassword);

var list = context.Web.Lists.GetByTitle("Docs");


CamlQuery query = CamlQuery.CreateAllItemsQuery();
ListItemCollection items = list.GetItems(query);

context.Load(items);
context.ExecuteQuery();

foreach (var item in items)


{
Console.WriteLine($"Title: {item["Title"]}, ID: {item["ID"]}, URL:
{item["FileRef"]}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
```

Question 2: FileLogger

```csharp
using System;
using System.IO;

public class FileLogger : ILogger


{
private readonly string _filePath;

public FileLogger(string filePath)


{
_filePath = filePath;
}

public void Log(string message)


{
try
{
string logMessage = $"{DateTime.Now}: {message}";
File.AppendAllText(_filePath, logMessage + Environment.NewLine);
}
catch (Exception ex)
{
Console.WriteLine($"Error writing to log: {ex.Message}");
}
}
}
```

Section 3: JavaScript

Question 1: validateFormData

```javascript
function validateFormData(formData) {
const errors = [];
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const phoneRegex = /^\d{10}$/;
const dateRegex = /^\d{4}-\d{2}-\d{2}$/;

if (!emailRegex.test(formData.Email)) errors.push("Email is invalid");


if (!phoneRegex.test(formData.PhoneNumber)) errors.push("PhoneNumber is invalid");
if (!dateRegex.test(formData.Date)) errors.push("Date is invalid");

return {
isValid: errors.length === 0,
errors,
};
}

// Example usage:
const formData = {
Email: "[email protected]",
PhoneNumber: "1234567890",
Date: "2025-01-01",
};
console.log(validateFormData(formData));
```

Question 2: API REST for Button in SharePoint

```javascript
document.getElementById("updateStatusButton").addEventListener("click", async () => {
const payload = { status: "approved" };

try {
const response = await fetch("/api/updateStatus", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});

if (response.ok) {
alert("Status updated successfully!");
} else {
alert("Error updating status.");
}
} catch (error) {
console.error("Error:", error);
alert("Error updating status.");
}
});
```

You might also like