Dynamics NAV

Wednesday, 21 June 2017

NAV 2016 - You do not have access to Microsoft Dynamics NAV - Create User From SQL stored procedure

Where we try to open the NAV2016 database,we might get the below error from Role Tailored Client -

You do not have access to Microsoft Dynamics NAV. Verify that you have been set up as a valid user in Microsoft Dynamics NAV







Copy the below stored procedure


USE [DATABASE NAME]
GO

/****** Object:  StoredProcedure [dbo].[createNAV2015User]    Script Date: 18-12-2015 13:54:51 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




-- =============================================
-- Description:     Add a user & login to SQL and add this to NAV
-- =============================================
CREATE PROCEDURE [dbo].[createNAV2016User]
       -- Add the parameters for the stored procedure here
       @Username varchar(200)
AS
BEGIN
       -- SET NOCOUNT ON added to prevent extra result sets from
       -- interfering with SELECT statements.
       SET NOCOUNT ON;
      
       DECLARE @BinSID AS VARBINARY(100)
       DECLARE @StringSID VARCHAR(100)
       DECLARE @i AS INT
       DECLARE @j AS INT  
       DECLARE @SQLString As Varchar(250)     
                  
       SELECT @BinSID = SUSER_SID (@Username,0)
   
       IF LEN(@BinSID) % 4 <> 0
       begin
             PRINT 'No valid SID found, exiting code'
             RETURN(NULL)
       end
      
       SELECT @StringSID = 'S-'
          + CONVERT(VARCHAR, CONVERT(INT, CONVERT(VARBINARY, SUBSTRING(@BinSID, 1, 1))))
       SELECT @StringSID = @StringSID + '-'
          + CONVERT(VARCHAR, CONVERT(INT, CONVERT(VARBINARY, SUBSTRING(@BinSID, 3, 6))))



       SET @j = 9
       SET @i = LEN(@BinSID)
       WHILE @j < @i
       BEGIN
         DECLARE @val BINARY(4)
         SELECT @val = SUBSTRING(@BinSID, @j, 4)
         SELECT @StringSID = @StringSID + '-'
             + CONVERT(VARCHAR, CONVERT(BIGINT, CONVERT(VARBINARY, REVERSE(CONVERT(VARBINARY, @val)))))
         SET @j = @j + 4
       END   
      
       BEGIN TRY
    DECLARE @GUID nvarchar(50)
    SET @GUID = NEWID()
    INSERT INTO [dbo].[User]
    ([User Security ID], [User Name], [Full Name], [State], [Expiry Date], [Windows Security ID], [Change Password], [License Type], [Authentication Email], [Contact Email])
    VALUES
    (@GUID, @Username, '', 0, N'1753-01-01', @StringSID, 0, 0, '', '')
    INSERT INTO [dbo].[User Property]
    ([User Security ID], [Password], [Name Identifier], [Authentication Key], [WebServices Key], [WebServices Key Expiry Date], [Authentication Object ID])
    VALUES
    (@GUID, '', '', '', '', N'1753-01-01', '')
    INSERT INTO [dbo].[Access Control]
    ([User Security ID], [Role ID], [Company Name], [App ID], [Scope])
    VALUES
    (@GUID, 'SUPER', '', '00000000-0000-0000-0000-000000000000', 0)
             IF EXISTS(SELECT TOP 1 1 [User Security ID] FROM [Access Control] WHERE [User Security ID] = @GUID)
                    PRINT 'User added to Windows logins in Dynamics NAV as SUPER'            
       END TRY
       BEGIN CATCH
             PRINT ERROR_MESSAGE();
             RETURN(NULL);      
       END CATCH
END

GO


and  open the SQL Server Management Studio(SSMS) and right click on the data base--> New Query and paste the copied stored procedure





change the database name and execute the stored procedure






then you can find the stored procedure in the below path





and right click on the stored procedure and execute and give your user id. User id will be created.