Showing posts with label folder. Show all posts
Showing posts with label folder. Show all posts

Monday, May 8, 2023

How to create a new folder on Sharepoint

There are some useful code behind OfficeSharePointFolderSelectionDiaolog form; however, there is a limitation.


Well, we can create a new folder via REST API call as follows. Take note that the access token is created in testSharePointConnection

using Microsoft.Dynamics.Platform.Integration.SharePoint;
using System.Net;
using System.Net.Http;

[Form]
public class wzhSPFolderForm extends FormRun
{
    str                 apiUrl;
    System.Exception    ex;
    ISharePointProxy    proxy;


    public void init()
    {
        super();
        fNewFolder.text('_wzhNewFolder');
        fServer.text('https://wzhServer.sharepoint.com');
        fRootFolder.text('Shared Documents');
    }

    public boolean testSharePointConnection()
    {
        str src = fServer.text();
        boolean validConnection = false;

        if(src)
        {
            System.UriBuilder   builder     = new System.UriBuilder(src);
            str                 hostName    = builder.Host;
            str                 siteName    = fRootFolder.text();
            str                 externalId  = xUserInfo::getExternalId();
            
            proxy   = SharePointHelper::CreateProxy(hostName, '/', externalId);
            if(proxy)
            {
                if(SharePointHelper::VerifyAuthentication(proxy))
                {
                    validConnection = true;
                    info(strfmt('@ApplicationFoundation:SPServerCommunicationSuccess', hostName));
                }
                else
                {
                    info(strfmt('@ApplicationFoundation:SPServerUserNotAuthorized', hostName));
                }
            }
            else
            {
                info(strfmt('@ApplicationFoundation:SPSelectionDlg_ErrorNoProxy', hostName));
            }
        }

        return validConnection;
    }

    public str GetAuthorizationToken()
    {
        return proxy.AccessToken;
    }

    public void createSPFolder()
    {
        apiUrl = strFmt("%1/_api/web/lists/getbytitle('%2')/rootfolder/folders/add('%3')", fServer.text(), fRootFolder.text(), fNewFolder.text());
        try
        {
            if(this.testSharePointConnection())
            {
                str token = this.GetAuthorizationToken();
            
                System.Net.WebHeaderCollection  httpHeader  = new System.Net.WebHeaderCollection();
                httpHeader.Add("Authorization", 'Bearer ' + token);

                HttpWebRequest                  request     = System.Net.WebRequest::Create(apiUrl);
                request.set_Headers(httpHeader);
                request.set_ContentLength(0);
                request.set_Method("POST");
                request.set_ContentType("application/json;odata=verbose");

                System.Net.HttpWebResponse  response        = request.GetResponse();
                int                         statusCode      = response.get_StatusCode();

                info(strFmt("HTTP status code: %1", statusCode));
            }
        }
        catch
        {
            //exception
            ex = CLRInterop::getLastException().GetBaseException();
            error(ex.get_Message());
        }
    }

    [Control("Button")]
    class bCreateREST
    {
        /// <summary>
        ///
        /// </summary>
        public void clicked()
        {
            element.createSPFolder();
            super();
        }

    }

    [Control("Button")]
    class bTestConnection
    {
        /// <summary>
        ///
        /// </summary>
        public void clicked()
        {
            element.testSharePointConnection();
            super();
        }

    }

}

Wednesday, November 16, 2016

Automate Error 351

From time to time we get the lovely CIL compilation Error:351. As suggested by many, for example, by André Arnaud de Calavon, we have to recreate XppIL folder with all its guts.

The sequence is the following (quotation)

1. Stop the AOS.
2. Rename the XppIL folder (C:\Program Files\Microsoft Dynamics AX\60\Server\MicrosoftDynamicsAX\bin\XppIL) to e.g. XppIL_old.
3. Start the AOS.
4. Perform a full CIL generation.
A new XppIL folder will be created when you start the AOS.
When the CIL has completed without problems, you can delete the renamed XppIL_old folder.

Clean and simple. However, I am too impatient to wait for the end of deleting this huge folder: Windows Explorer starts time estimation and it drags on.

So, I wrote two short batch files that can be run as administrator and spare your time and nerves.

First just to rename the existing folder.


@echo off
set folder="C:\Program Files\Microsoft Dynamics AX\60\Server\CHR_AX_DEV\bin\"
echo Rename %folder%  XppIL to XppIL_old?
pause
c:
cd %folder% 
ren XppIL XppIL_old
echo %folder%XppIL to  has been renamed to XppIL_old
pause

Second to delete all the files in the 'backed up' folder and its subfolders with no questions and infolog, then to delete the folder itself. As it said here, they must work faster than just removing the folder.

@echo off
set folder="C:\Program Files\Microsoft Dynamics AX\60\Server\CHR_AX_DEV\bin\XppIL_old\"
echo Delete all the files from %folder% and remove the folder?
pause
del /f/s/q %folder% > nul
rmdir /s/q %folder%
echo Folder %folder% removed
pause

The last remarque. Be sure that your service windows account running the AOS in question had Full permission to the C:\Program Files\Microsoft Dynamics AX folder and all its subfolders.

command line