Search This Blog

Sunday, January 24, 2021

Update App Config During Runtime In C#

NameValueCollection sAll = ConfigurationManager.AppSettings;

            foreach (string str in sAll.AllKeys)

            {

                if (str == appkey)

                {

                    label2.Text = sAll.Get(str);

                }

            } 


// Get AppSettings section from the Config

            Configuration config = ConfigurationManager.OpenExeConfiguration(this.GetType().Assembly.Location);

            AppSettingsSection App_Section = (AppSettingsSection)config.GetSection("appSettings");

            KeyValueConfigurationCollection app_settings = App_Section.Settings;

            KeyValueConfigurationElement element = app_settings[appkey];

            // Grab Key-Value pair & Make your Change

            if (element != null)

            {

                //Create the object

                if (element.Value != label2.Text)

                {

                    element.Value = label2.Text;

                    app_settings.Remove(appkey);

                    app_settings.Add(element);

                    //save to apply changes

                    config.Save();

                    ConfigurationManager.RefreshSection("appSettings");

                    MessageBox.Show("Succeefully changed the Link.\nPlease exit Application and reopen to change reflection..!");                  

                    this.Close();                   

                }   

            }

            else

            {

                MessageBox.Show("Setting not Modified");

            }


Saturday, January 23, 2021

SharePoint online - Multi attachments- inside list

 //change event

  <div class="form-group col-md-6">
  <span id="attach">
      <input id="uploadFileID" type="file" ng-file-model="files" multiple />                                               
      <span class="attach-Text" ng-repeat="file in files">
          <span>{{file.name}}</span><a title='Click to Remove' ng-click="removeFile(file, $index)"X </a>                                                    
      </span>
  </span>
</div>
--------------------------------Controller
$scope.name = 'UploadAttachements';
$scope.files = [];
$scope.removeFile = function (attachedFileindex) {
    var removedItem = $scope.files.indexOf(attachedFile);
    $scope.files.splice(removedItem1);
};       
$scope.saveAttachment = function (ListName,ListItemId) {
    if ($scope.files.length > 0) {
        var numberOfFiles = $scope.files.length;
        angular.forEach($scope.filesfunction (fileValueskey) {
            getFileBuffer(fileValues._file)
                .then(function (bufferVal) {
                    uploadFileSP(bufferValfileValues._file.nameListNameListItemId);
                    numberOfFiles--;
                    if (numberOfFiles == 0) {
                        console.log("attachment insert success");
                    }
                });
        });
    }
    else {
        alert('no files');
    }
}
function uploadFileSP(bufferValfileNamelistNameitemID) {         
      var urlValue = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('" + listName + "')/items(" + itemID + ")/AttachmentFiles/add(FileName='" + fileName + "')";

      $.ajax({
          url: urlValue,
          type: "POST",
          data: bufferVal,
          async: false,
          processData: false,
          headers: {
              "X-RequestDigest": $("#__REQUESTDIGEST").val(),
              "accept": "application/json;odata=verbose",
              "content-type": "application/json; odata=verbose"
          },
          success: fileSuccess,
          error: fileError
      });
      function fileSuccess(data) {
          console.log('File Added Successfully.');
      }
      function fileError(error) {
          console.log(error.statusText + "\n\n" + error.responseText);
      }
  }
  function getFileBuffer(file) {
      var deferred = $.Deferred();
      var reader = new FileReader();
      reader.onloadend = function (e) {
          deferred.resolve(e.target.result);
      }
      reader.onerror = function (e) {
          deferred.reject(e.target.error);
      }
      reader.readAsArrayBuffer(file);
      return deferred.promise();
  }
------------------------- add directive--------------------------
 app.directive('ngFileModel', ['$parse'function ($parse) {
      return {
          restrict: 'A',
          link: function (scopeelementattrs) {
              var model = $parse(attrs.ngFileModel);
              var isMultiple = attrs.multiple;
              var modelSetter = model.assign;
              element.bind('change'function () {
                  var values = [];
                  angular.forEach(element[0].filesfunction (item) {
                      var value = {
                          // File Name
                          name: item.name,
                          //File Size
                          size: item.size,
                          //File URL to view
                          url: URL.createObjectURL(item),
                          // File Input Value
                          _file: item
                      };
                      values.push(value);
                  });
                  scope.$apply(function () {
                      if (isMultiple) {
                          modelSetter(scopevalues);
                      } else {
                          modelSetter(scopevalues[0]);
                      }
                  });
              });
          }
      };
  }]);





Friday, January 15, 2021

Find Last 5 Weeks Number in C#

public static int GetIso8601WeekOfYear(DateTime time)
{
    DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
    if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
    {
        time = time.AddDays(3);
    }

    return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(timeCalendarWeekRule.FirstFourDayWeekDayOfWeek.Monday);
}

 int week1 = GetIso8601WeekOfYear(DateTime.Today);

int week2 = GetIso8601WeekOfYear(DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek - 6));
int week3 = GetIso8601WeekOfYear(DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek - 12));
int week4 = GetIso8601WeekOfYear(DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek - 18));
int week5 = GetIso8601WeekOfYear(DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek - 21));