Sample code to check if a Folder exists else create it (SharePoint Online / PnP Framework / C#)


Sharing the sample code that we can use for checking if a particular folder exists and if not then create it in SharePoint Online. It uses PnP Framework and Azure Ad App only permissions.

Here we have used the PnP Framework Library in our console application.

We will be checking for and creating the folder at the below location.

A screenshot of a computer

Description automatically generated

On a successful run, we can see the folder named “MyFolder” created the document library.

Below is the sample code

static void Main(string[] args)
        {
            var siteUrl = "https://w72tk.sharepoint.com/sites/MyTeamSite";          
            var applicationId = "d7eaeeb7-ef0a-474d-9b94-567013576c14";
            var password = "xyz";
            var domain = "w72tk.onmicrosoft.com";
            var certPath = @"C:\SharePointApp\MyTestCertificate.pfx";

            var authManager = new AuthenticationManager(applicationId, certPath, password, domain);
            var foldertoCheck = "MyFolder";
            using (var clientContext = authManager.GetContext(siteUrl))
            {
                var currentWeb = clientContext.Web;
                var folderExists = currentWeb.DoesFolderExists(foldertoCheck);
                if (!folderExists)
                {
                    var list = clientContext.Web.Lists.GetByTitle("Documents");
                    list.RootFolder.Folders.Add(foldertoCheck);
                    clientContext.ExecuteQuery();
                }
            }
        }

Refer for more details – https://nishantrana.me/2024/07/30/calling-sharepoint-online-api-using-azure-ad-app-only-permissions-using-certificate-auth/

Hope it helps..

Advertisements

Based on entity behavior for SharePoint Folder – Dynamics 365 / Dataverse


After enabling Server-Based SharePoint Integration,

inside Document Management Settings, we can specify folder structure to be based on the entity either Account or Contact.

Here we have opted for Account-based first.

We can see the following folders created on the SharePoint site, that take the table or entity name.

And the records folder for Accounts created with AccountName_GUID format.

And if we open any Contact record >> Files / Documents tab, that will create the account parent folder (if not already created) and the contact folders inside it as shown below.

And if we create the contact record without having an account associated

That will create the folder for the record inside Contact Folder without the Parent Account folder created. (as there was no account associated)

For Case Records, we have the case folders created inside Account folders.

Now let us select the Contact based structure

The contact records are created inside the Contact folder, the account associated is not considered.

For Case also it is same.

For Building, a custom table, which is a child of Contact, we have the Building folder created inside the Contact folder.

Now let us keep Based on entity option unchecked

This creates the corresponding record’s folder inside the table parent folder.

Account –

Contact –

Check some of the interesting articles on SharePoint and Dynamics 365 Integration.

Hope it helps..

Advertisements

Fixed – “A type named ’SP.Data.ListItem’ could not be resolved by the model. When a model is available, each type name must resolve to a valid type” error in SharePoint 2013.


Hi,

Was getting the above error while creating a listitem for a Tasks List using Rest interface in a SharePoint app.

Here we need to pass SP.Data.<ListName>ListItem i.e. ListEntityTypeFullName of the List.

We can get it using the following

<a href="https:///_api/web/lists/getbytitle(&#8221;)?$select=ListItemEntityTypeFullName”>https://<site>/_api/web/lists/getbytitle(‘<List Name>’)?$select=ListItemEntityTypeFullName

More details

http://msdn.microsoft.com/en-us/library/jj164022%28office.15%29.aspx#ListItems

Hope it helps.

Advertisements

appweburl or SPAppWebUrl is undefined error in SharePoint 2013 Auto Hosted Apps


Hi,

I was getting the “appweburl” is undefined error while developing a SharePoint Auto Hosted App.


// Initialize the RequestExecutor with the app web URL.

executor = new SP.RequestExecutor(appweburl);


//Get the URI decoded URLs.

hostweburl =

decodeURIComponent(

getQueryStringParameter(“SPHostUrl”)

);


appweburl =

decodeURIComponent(

getQueryStringParameter(“SPAppWebUrl”)

);

 

The value of hostweburl was getting passed in the querystring properly, but the value for appweburl was not getting passed.

The way to resolve this issue was to add an empty element to the App.

By default the Autohosted or Provider-hosted template have an empty app web. If the app web is empty SharePoint doesn’t create the app web for you.

That’s how simply adding an empty element fixes the issue.

The post that came to rescue.

http://rainerat.spirit.de/2012/10/05/sharepoint-2013-apps-csom-vs-rest/

Hope it helps.