Monday, September 11, 2017


Recently I stacked on https related error on .NET Core 2.0

- InnerException {System.Net.Http.WinHttpException (0x80072F8F): A security error occurred
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.Tasks.RendezvousAwaitable`1.GetResult()
   at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext()} System.Exception {System.Net.Http.WinHttpException}

All that I could found - this error is related to the inability of creation SSL/TLS tunnel.
But could be caused by different reasons:

  1. require explicitly set TLS - https://stackoverflow.com/questions/42746190/https-request-fails-using-httpclient
  2. skip invalid certificate check - https://stackoverflow.com/questions/42746190/https-request-fails-using-httpclient
  3. an absence of common cipher methods between server and client
Or some different reasons.
The core problem - this error is vague in .NET Core, when I tested this on .NET Framework the error was much more clear "The request was aborted: Could not create SSL/TLS secure channel."

In my case, this was an absence of common cipher methods between server and client. So I tracked by Fiddler SSL/TLS handshake and ask server admins to add on server configuration one of the cipher methods from client SSL/TLS handshake.

Monday, November 25, 2013

PowerShell password generator

Generating a password really isn’t too tricky when you think about it. I can run the following cmdlet:
GET-RANDOM
It will immediately produce a 10 character random numeric number. Sure, it could be a really cool pin code for somebody’s voicemail or a really lousy login Password.
Get-Random can even work with random data from an array such as this:
“dog”,”cat”,”rubber chicken” | GET-RANDOM
So we could improve our random Password by submitting a string containing the entire alphabet to the Get-Randomcmdlet. First, we generate a variable that contains all of the uppercase characters in the alphabet as a start.
$alphabet=$NULL;For ($a=65;$a –le 90;$a++) {$alphabet+=,[char][byte]$a }
I could then loop through this, say 10 times, to generate a 10-character password. We can build a simple function to meet this need, and supply the raw data and the length of the password as parameters.
Function GET-Temppassword() {
Param(
[int]$length=10,
[string[]]$sourcedata
)

For ($loop=1; $loop –le $length; $loop++) {
            $TempPassword+=($sourcedata | GET-RANDOM)
            }
return $TempPassword
}
Now we could call up a function to generate a simply random password like this:
GET-Temppassword –length 19 –sourcedata $alphabet
YYEGQXCBHTHOBIHSGDL
This works well, but only produces a password of UPPERCASE characters. This is hardly anything that would meet most security guidelines. So we can build a bigger character set. We could modify our “Building the alphabet loop,” starting with the first available ASCII character until we basically run out.
$ascii=$NULL;For ($a=33;$a –le 126;$a++) {$ascii+=,[char][byte]$a }
Then we plug this into our function for a Temporary password. Let’s have some fun and hand our user a 43-character password!
GET-Temppassword –length 43 –sourcedata $ascii
XWsX=yxlJxRW85#dF9'#eu%Qe[jjRZbzCU&M+w6"_*H
Wow! I think we’ve definitely hit the “Password complexity” rules, but I suspect that although Security will be dancing with joy, our Help Desk will get hit severely with staff mistyping this new password.
Of course, simply by limiting the number of characters, we might have a more palatable password…say 9?
GET-Temppassword –length 9 –sourcedata $ascii
tPl/kN-%R
Or to really balance things down, we could refine the list of characters to Mostly Alphabetic, Numeric, and a few “Specials” by picking a sequence of ASCII characters that meet our needs.
$ascii=$NULL;
For ($a=48;$a –le 122;$a++) {$ascii+=,[char][byte]$a }
Running with this combination, we get a slightly more palatable password:
l8_[mzx[u
Now here’s another neat trick...
You can pipe this function to a built-in feature in Windows 7 and Windows 8, called clip.exe. This will take the output and place it directly onto the clipboard.
GET-Temppassword –length 9 –sourcedata $ascii | CLIP
Or if you want to generate the password as something useful for a New user, and the cmdlet requires that the password is in a secure-string format, you could do something like this to save it, clip it, and make it secure:
$PW= GET-Temppassword –length 9 –sourcedata $ascii | CLIP
$PW | CLIP
$SecurePW=CONVERTTO-Securestring $PW -asplaintext -force
How you build the source data for generating a password is up to you. There are some excellent scripts in the Script Repository for building passwords in a myriad of ways. What’s important for you, is that you can generate them relatively easily and in methods under your control.
I would suggest avoiding enforcing the 43-character minimum as the limit. Just sayin’…

Fix temp profile cteating in Windows 7

Go to:
HKLM\SOFTWARE\MICROSOFT\WINDOWS NT\CURRENT VERSION\PROFILE LIST

delete profiles with .bak and they will be recreated after next login.
Also may be needed delete profiles from control panel - system - advanced system settings - advanced (tab)- user profiles (block on tab) - settings (this will delete all folders for specific acc, so if if needed files must be backuped)

Monday, November 18, 2013

CAML (Collaborative Application Markup Language) for SharePoint query

SPQuery is the SharePoint object which is used to perform a query operation against SharePoint data.SPList.getItems(SPQuery) is the step, will return SPListItemCollection which satisfies the query.SPQuery has one data member ‘Query’, which need to set before passing SPQuery object to SPList.
Some Properties
Properties
Description
RowLimit
Gets or sets a limit for the number of items returned in the query per page.
<RowLimit>10</RowLimit>
---
Can be used as: query.rowLimit = 10;
ViewFields
Gets or sets the fields that are displayed in the query.
<ViewFields>
        <FieldRef Name='Title'/>
        <FieldRef Name='Name'/>
</ViewFields>
---
Can be used as:
 SPQuery query = new SPQuery();
 query.ViewFields = "<FieldRef Name='Field1'/>" +
                      "<FieldRef Name='Field2'/>";   
How to Implement SPQuery?
How to Implement SPQuery?
using (SPWeb web = SPContext.Current.Site.RootWeb)
{
    SPList mylist = web.Lists["Tasks"];
    SPQuery query = new SPQuery();
    query.Query = "<Where><Eq><FieldRef Name='Status'/>" +
                   "<Value Type='Text'>Completed</Value></Eq></Where>";
    SPListItemCollection items = mylist.GetItems(query);
    foreach (SPListItem item in items)
    {
        Response.Write(SPEncode.HtmlEncode(item["Title"].ToString()) + "<BR>");
    }
}
Operators:
Comparison Operators, Logical Joins and Order/ Group Operators, plays an important role to make this syntax.

Comparison Operators
Comparison Operators
General Meaning
Eq
=
Gt
> 
Lt
< 
Geq
>=
Leq
<=
Neq
<> 
Contains
Like
IsNull
Null
IsNotNull
NotNull
BeginsWith
Beginning with word
DateRangesOverlap
compare the dates in a recurring event with a specified DateTime value, to determine whether they overlap

Now, to use Or between Geq and Leq conditions, we put it inside <Or> tag.
I.e. condition: where (Field1 >= 1500) or (field2 <= 500) can be written like this:
   <Where>
  <Or>
     <Geq>
         <FieldRef Name='Field1'/>
         <Value Type='Number'>1500</Value>
     </Geq>
     <Leq>
        <FieldRef Name='Field2'/><Value Type='Number'>500</Value>
     </Leq>
   </Or>
  </Where>
The full Code snippets will look like:
<View>
 <Query>
      <OrderBy>
          <FieldRef Name='ID'/>
      </OrderBy>
 
  <Where>
  <Or>
     <Geq>
         <FieldRef Name='Field1'/>
         <Value Type='Number'>1500</Value>
     </Geq>
     <Leq>
        <FieldRef Name='Field2'/><Value Type='Number'>500</Value>
     </Leq>
   </Or>
  </Where>
 </Query>
   <ViewFields>
        <FieldRef Name='Title'/>
        <FieldRef Name='Name'/>
   </ViewFields>
   <RowLimit>10</RowLimit>
</View>
Example: Contains, And, BeginsWith
The following example uses the Contains element that is assigned to the Query property to return the titles of items where the Conference column value begins with "Morning" and contains "discussion session".
<Where>
   <And>
       <BeginsWith>
              <FieldRef Name="Conference"/> 
              <Value Type="Note">Morning</Value>
       </BeginsWith>
       <Contains>
              <FieldRef Name="Conference" />
              <Value Type="Note">discussion session</Value>
       </Contains>
   </And>
</Where>

Example: DateRangesOverlap
<Where> 

     <DateRangesOverlap> 

          <FieldRef Name="EventDate"></FieldRef> 

          <FieldRef Name="EndDate"></FieldRef> 

          <FieldRef Name="RecurrenceID"></FieldRef> 

            <Value Type="DateTime"> 

              <Now/> 

            </Value> 

     </DateRangesOverlap> 

</Where>
Logical Joins:
Logical Joins
Comments
And
Used within the ‘Where’ element to group filters in a query for a view
Or
Used within the ‘Where’ element to group filters in a query for a view
Example: And
<Where>

   <And> 

     <Neq> 

        <FieldRef Name="Status"></FieldRef> 

        <Value Type="Text">Completed</Value> 

     </Neq> 

     <IsNull> 

        <FieldRef Name="Sent"></FieldRef> 

     </IsNull> 

   </And> 

</Where> 
Order/Group Operators:
Order/Group Operators
Comments
OrderBy
Determines the sort order for a query. The OrderBy element contains a group of FieldRef elements
I.e. <OrderBy><FieldRef Name="Title" Ascending="TRUE">
</FieldRef></OrderBy>

GroupBy
Contains a Group By section for grouping the data returned through a query in a list view
Example: OrderBy
<OrderBy> 

       <FieldRef Name="Modified" Ascending="FALSE"></FieldRef> 

</OrderBy> 

<Where> 

  <Or> 

    <Neq> 

      <FieldRef Name="Status"></FieldRef> 

      <Value Type="Text">Completed</Value> 

    </Neq> 

    <IsNull> 

      <FieldRef Name="Status"></FieldRef> 

    </IsNull> 

  </Or> 

</Where>
Example: GroupBy
<GroupBy> 

      <FieldRef Name="Modified"/> 

</GroupBy> 

<Where> 

   <Or> 

      <Neq> 

          <FieldRef Name="Status"></FieldRef> 

          <Value Type="Text">Completed</Value> 

      </Neq> 

      <IsNull> 

          <FieldRef Name="Status"></FieldRef> 

      </IsNull> 

   </Or> 

</Where> 
Important:
This is very important thing to keep in mind, because SPQuery will be assigned as a string so it wont show compile time error though it may have tag syntax error, not in order, escape sequence error, etc.
Escape sequence:
The ScriptEncode method escapes characters that would otherwise conflict with script.
Original
Replacement
"
\"
\
\\
+
\u002b
> 
\u003e
< 
\u003c
'
\u0027


 
Example:

<FieldRef Name=\"Checkbox\"></FieldRef> <Value Type=\"bit\">1</Value> 

Creating a Custom Filter Dropdown for a SharePoint List Using InfoPath

Almost all of the SharePoint power users and developers are undoubtedly familiar with the web part connectivity feature of SharePoint. This has been an out-of-the-box feature that was available in SharePoint 2007, SharePoint 2010 and is still available in SharePoint 2013. Although, we all find this feature very useful, quick and easy to implement, this might not fulfill our requirements all the time, especially, if the filter dropdown that we need must be in a customized fashion. Let’s take for instance a requirement where user needs to select a Region (E.g. Europe) and then its respective Country should be cascaded in another list. After selecting a Country (E.g. Italy), its respective cities should then be cascaded in another list. And then, upon selecting City (E.g. Rome), the items related to that city is then filtered in another list, for example Hotel names as used scenario in this article.
In this article, I will be drawing attention to 2 solutions that will be beneficial for SharePoint power users or non-developers who have requirements on cascading dropdown list and custom filter dropdown. I am able to achieve this solution with the use of InfoPath Form Designer 2010 with SharePoint 2010. Although this solution is also achievable also using InfoPath Form Designer 2007, however, the steps provided below might not be the same as interface are a bit different.
Below is the summary of the procedures to implement this solution.
a. Create a custom list where the regions, countries and cities will be looked up.
b. Create a custom list where cascading list items will be applied using InfoPath form.
c. Customize a list through InfoPath Form Designer to implement the cascading dropdown list.
d. Create a new list where custom filter will be applied using InfoPath Form Designer.
e. Insert InfoPath form on a page and apply web part connection between the form and the list web part to apply filter.
After complementing the steps, you should be able to achieve the 2 features shown on the screenshots below.
Cascading Dropdown list on a New Form
 Cascading Dropdown.png
Custom Filter Dropdown
Custom Filter Dropdown.png 
a. Create a custom list where the regions, countries and cities will be looked up.
1. Create a custom list with the list name, Hotel-Country-City.
2. Create the columns listed in the table below.
Column
Type
Region
Single line of text
Country
Single line of text
City (Title)
Single line of text
 
3. Enter sample items in the Hotel-Country-City list with the items shown in the screenshot below. Two items per Region will also do.
Sample Items.png 
b. Create a custom list where cascading list items will be applied using InfoPath form.
1. Create a custom list with the list name, Hotels.
2. Create the columns listed in the table below.
ColumnType
Hotel Name (Title)Single line of text
RegionSingle line of text
CountrySingle line of text
CitySingle line of text
 
c. Customize a list through InfoPath Form Designer to implement the cascading dropdown list.
1. On the Hotels list, under the List Tools, click List tab > Customize Form.
2. InfoPath Form Designer is then launched with the Hotels form controls displayed on the page.
3. On the InfoPath Form Designer window, click Data tab > Data Connections > click Add
4. On the Data Connection Wizard > Create a new connection to: > select Receive Data > click Next.
5. On the Select the source of your data > From where you want to receive your data? > select SharePoint library or list.
6. On the SharePoint site details > Enter the location of the SharePoint site: > the URL of the list location is already populated. (E.g. [WebURL]/[List]) > click Next
7. On the Select a list or library: > select “Region-Country-City” list > click Next
8. On the Select fields: > tick Region, Country and City fields > click Next
9. Verify if the list name is displayed on Enter a name for this data connection and click Finish
10. Verify if the Region-Country-City is already added as one of the Data Connections and then Close.
11. Right click Region form control > Change Control > Dropdown list box.
12. Right click again on the Region form control > Dropdown list box properties > on the List box choices section > select Get choices from an external data source.
13. On the Data Source field, select Region-Country-City list.
14. Verify if the Value and Display Name are both Region.
15. Put checkmark on the “Show only entries with unique display names”.
Checkmark.png 
16. Click Apply and OK.
17. Do steps 11-13 for the Country form control.
18. On the Entries section click Select XPath button.
19. Expand the “SharePointListItem_RW” and then select Country.
Select Country.png 
20. While Country is selected click Filter Data… button. On the Filter Data window, click Add button.
21. On Specify Filter Conditions window > on the drop down on the left side, select Region.
22. Verify if the dropdown menu in the middle is set to “is equal to”.
23. On the right dropdown menu, click “Select a field or a group…” option.
24. On the Data Source section, click the dropdown menu and select Main.
25. Expand “dataFields” and then expand “SharePointListItem_RW”, select Region
26. On the Fields section select Region. Click OK
Select Region.png 
27. In the Filter Data win​dow, click OK.
Filter Data Window.png 
28. Click OK twice.
29. Do steps 11-28 for City form control. Note that for the City control, the filter is Country = Country
City Form Control.png 
30. Click Region form control > Properties tab > Field Properties > Default Value > type in the value “Select…”
31. Do step 30 for Country and City controls.
32. Click Region form control again> Properties tab > Rules section > Manage Rules
33. Click New dropdown > select Validation.
34. Enter “Select” on the Details for: section.
35. On the Condition section, click None link and then set the condition, Region is not equal to “Select…” (Type text…). Click OK.
36. On the Rule type: > select Action
37. On the Run these actions: click Add button > Set a field’s value > Select the Country field and type “Select…” on the Value: section > click OK.
Action.png 
38. Do step 37 to set value “Select…” for City control.
Select.png 
39. Click Country form control > Properties tab > Rules section > Manage Rules
40. Click New dropdown > select Validation.
41. Enter “Select” on the Details for: section.
42. On the Condition section, then click None link and then set the condition, Country is not equal to “Select…” (Type text…)
43. On the Rule type: > select Action
44. On the Run these actions: click Add button > Set a field’s value > Select the City field and type “Select…” on the Value: section > click OK.
45. Click Save and Publish
46. Test the list new form by adding new item in the list.
d. Create a new list where custom filter will be applied using InfoPath Form Designer.
1. Create a custom list with the list name, Filter.
2. Create the columns listed in the table below.
Column
Type
Region
Single line of text
Country
Single line of text
City (Title)
Single line of text
 
3. Follow the steps in section c, steps 1-46. Note that on step 7, you should select Hotels list.
4. Still on InfoPath Form Designer, select City field > click Properties tab > Manage Rules > click New dropdown > click Action.
5. On the Details for: section, type in “Send data”. Leave the condition to None.
6. Click Add button and then select Send data to Web Part.
Rules.png 
7. On the Rule Details pop window, click OK.
8. Save and Publish the form.
e. Insert InfoPath form on a page and apply web part connection between the form and the list web part to apply filter.
1. Create a new page and give it a name (E.g. Hotels Filter).
2. Edit the page and click Add a Web Part button.
3. On the Categories column, select List and Libraries
4. On the Web Parts column, select Hotels
5. While page is still in edit mode, click Add a Web Part button.
6. On the Categories column, select Forms.
7. On the Web Parts column, select InfoPath Form Web Part.
8. On the InfoPath Form Web Part inserted on the page, click the link, “Click here to open the tool pane”
9. On the InfoPath Form Web Part properties, on the List or Library section, select Filter.
10. Uncheck the “Show InfoPath Ribbon or toolbar”.
11. Leave the rest of the properties to default and then click OK.
12. On the Hotels list web part menu, click Connections > Get Filter Values From > Filter
13. On the Connection Settings > On the Provided and Consumer Field Name select, “City” for both. Click Finish.
14. Save and Close and Check In the page. Publish the page if applicable.
15. Test the solution by selecting Region and then Country, and then City, and the list should be filtered accordingly.
The above solution doesn’t really entail much of an effort to implement as InfoPath Form Designer is very intuitive software to use. It’s just a matter of getting yourself familiar with the interface on where to find what property or feature and how to use it in SharePoint environment. If this is your first time to use InfoPath, this could be a good start for you discover the power of InfoPath Form Designer with SharePoint. For those who already have experience using InfoPath Form Designer in SharePoint, I hope my solution helps you achieve your requirements as much as how I was able to accomplish mine.